• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

send mail with attachement

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = {"[email protected]"};
String attachfile = "C:/filelocation.htm";
try
{
mail.sendMail(address,"[email protected]","filw with attachment","test Msg",attachfile);
System.out.println("Mail Sent");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Thanxs in advance
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Other Java APIs.
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched for related topics in this forum itself and got the solution for my problem. Thanxs every one..!
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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="[email protected]";
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());
}

}

*/
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic