jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "send mail with attachement" Watch "send mail with attachement" New topic

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)
public TestMail()

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);
MimeBodyPart mbp2 = new MimeBodyPart();
Multipart mp = new MimeMultipart();
Session mailSession = Session.getDefaultInstance(props, null);
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.setSentDate(new Date());
msg.setHeader("X-Mailer", "Efficacy Solutions Example Mailer");
javax.activation.DataSource fds = new javax.activation.FileDataSource(attachfile);
mbp2.setDataHandler(new javax.activation.DataHandler(fds));

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";
mail.sendMail(address,"emailaddress@email.com","filw with attachment","test Msg",attachfile);
System.out.println("Mail Sent");
catch(Exception e)

Thanxs in advance
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63355

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{
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";

SmtpAttachment example = new SmtpAttachment();
}catch(Exception e)
String url1="/ap/mail/mail_sent.jsp?";
String url=url1+to;

}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

// capture SMTP related events

// connect to mail server

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

// set recipient address

// set from address

// set subject

// set body

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

// send email message

// disconnect from SMTP server

// 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());


I agree. Here's the link: http://aspose.com/file-tools
subject: send mail with attachement
It's not a secret anymore!