This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes send mail with attachement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "send mail with attachement" Watch "send mail with attachement" New topic
Author

send mail with attachement

Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
hi... This is the code that i'm using to send a mail with attachment .. The problem that im facing with this piece of code is that 'send mail' method is not sending the text part... all other details with attachment r going fine ... but the text 'test msg ' is not going... can some plz tell me where i'm going wrong in sending the text..!!!

import java.io.*;
import java.util.*;
import java.net.InetAddress;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.MessagingException;
import javax.mail.Store;
import javax.activation.DataSource.*;


public class TestMail
{
private String smtpServer = "smtpname";
public TestMail(String smtpServer)
{
this.setSmtpServer(smtpServer);
}
public TestMail()
{
this("smtpname");
}

public synchronized void sendMail(String [] addresses, String from, String subject,String text, String attachfile)
throws MessagingException
{
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.debug","false");
MimeBodyPart mbp2 = new MimeBodyPart();
Multipart mp = new MimeMultipart();
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(false);
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] intAddresses = new InternetAddress[addresses.length];
for (int i = 0; i < addresses.length; i++)
{
intAddresses[i] = new InternetAddress(addresses[i]);
}
msg.setRecipients(Message.RecipientType.TO,intAddresses );
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setHeader("X-Mailer", "Efficacy Solutions Example Mailer");
msg.setText(text);
javax.activation.DataSource fds = new javax.activation.FileDataSource(attachfile);
mbp2.setDataHandler(new javax.activation.DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
msg.setContent(mp);
msg.saveChanges();
Transport.send(msg);
}

public void setSMTPServer(String server)
{
this.smtpServer = server;
}
public void setSmtpServer(String smtpServer)
{
this.smtpServer = smtpServer;
}
public String getSmtpServer()
{
return smtpServer;
}
public static void main(String arg[])
{
TestMail mail = new TestMail();
String[] address = {"xyz@hotmail.com"};
String attachfile = "C:/filelocation.htm";
try
{
mail.sendMail(address,"emailaddress@email.com","filw with attachment","test Msg",attachfile);
System.out.println("Mail Sent");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Thanxs in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Moving to Other Java APIs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
please can some one see the code and tell me where am i going wrong....!!! coz the text is not going when i send mail with attachment... the attachment goes but the text doesnot..!
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
I searched for related topics in this forum itself and got the solution for my problem. Thanxs every one..!
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
well its hard to read and debug ur code but if u want you can take mine.
I am successfully able to send mails and attachments.


/*
package mail;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Random;
import pk_globalfunctions.*;

public class send_mail extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try{
HttpSession session = request.getSession(true);
String p_no=request.getQueryString();
String dataSourceName = "al_db";
String dbURL = "jdbc dbc:" + dataSourceName;
Connection dbcon = DriverManager.getConnection(dbURL);
Statement stmt1 = dbcon.createStatement();
Statement stmt2 = dbcon.createStatement();


String hostname = request.getParameter("domain");
String to =request.getParameter("txtemail");
String from="admin@superplasticcoats.com";
String subject =request.getParameter("subject"); ;
String body = "see attached file";
String attach ="details.html";

try
{
SmtpAttachment example = new SmtpAttachment();
example.sendMessage(hostname,to,from,subject,body,attach);
}catch(Exception e)
{
System.out.println(e);
}
String url1="/ap/mail/mail_sent.jsp?";
String url=url1+to;
response.sendRedirect(url);

}catch(Exception e2){}
}
}



///

package mail;

import com.jscape.inet.smtp.*;
import com.jscape.inet.mime.*;
import com.jscape.inet.email.*;
import java.io.*;

public class SmtpAttachment extends SmtpAdapter {
public void sendMessage(String hostname, String to, String from, String subject, String body, String attachment) throws SmtpException,
IOException, MimeException {
System.out.println("reached here");
// create new Smtp instance
Smtp smtp = new Smtp(hostname);

// enable debugging ... all output is sent to System.out
smtp.setDebug(true);

// capture SMTP related events
smtp.addSmtpListener(this);

// connect to mail server
smtp.connect();

// create new email message
EmailMessage email = new EmailMessage();

// set recipient address
email.setTo(to);

// set from address
email.setFrom(from);

// set subject
email.setSubject(subject);

// set body
email.setBody(body);

// add file attachment to message
email.addAttachment(new Attachment(new File(attachment)));

// send email message
smtp.send(email);

// disconnect from SMTP server
smtp.disconnect();
}

// capture connect event
public void connected(SmtpConnectedEvent evt) {
System.out.println("Connected to Smtp server: " + evt.getHostname());
}

// capture disconnect event
public void disconnected(SmtpDisconnectedEvent evt) {
System.out.println("Disconnected from Smtp server: " + evt.getHostname());
}

}

*/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: send mail with attachement