sending a javamail
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   steve_wong
Posted On:   Wednesday, February 27, 2002 12:35 AM

can someone show me the code step by step of sending an e-mail using javamail. if possible included what does each lines do.

Re: multiple recipients sending a javamail

Posted By:   mahesh_kalluri  
Posted On:   Saturday, March 2, 2002 12:47 AM

/*
submit an html form which contains textboxes for "from,to,cc,bcc,subject,content" to this .jsp file.
Do change your mailhost name in this file.your message will be sent automatically.
*/


<%@ page import = "java.io.IOException,java.io.PrintWriter,java.sql.*,java.util.Hashtable,java.util.Properties" %>
<%@ page import =" javax.mail.*,javax.mail.internet.InternetAddress,javax.mail.internet.MimeMessage" %>
<%@ page import = "javax.servlet.*,javax.servlet.http.*" %>

<%

try
{
String actmailhost = "xxxxxx";//your mailhost
String actfrom = request.getParameter("from");
String actto = request.getParameter("to");
actto=actto+"-";
actto=actto.trim();

String actcc = request.getParameter("cc");
actcc=actcc+"-";
actcc=actcc.trim();

String actbcc = request.getParameter("bcc");
actbcc=actbcc+"-";
actbcc=actbcc.trim();

String actsubject = request.getParameter("subject");
String actcontent = request.getParameter("content");

Properties properties = new Properties();
properties.put("mail.smtp.host", actmailhost);
properties.put("mail.from", actfrom);

Session session1 = Session.getInstance(properties, null);
MimeMessage mimemessage = new MimeMessage(session1);

InternetAddress[] to=null;
InternetAddress[] cc=null;
InternetAddress[] bcc=null;

mimemessage.setFrom(new InternetAddress(actfrom));
mimemessage.setSubject(actsubject);
mimemessage.setContent(actcontent.toString(), "text/html");

Transport transport = session1.getTransport("smtp");
transport.connect();

if (!actto.equals("-"))
{
actto = actto.substring(0,actto.length()-1);
to = InternetAddress.parse(actto);
mimemessage.setRecipients(Message.RecipientType.TO, to);
transport.sendMessage(mimemessage, to);
}

if (!actcc.equals("-"))
{
actcc = actcc.substring(0,actcc.length()-1);
cc = InternetAddress.parse(actcc);
mimemessage.setRecipients(Message.RecipientType.CC, cc);
transport.sendMessage(mimemessage, cc);
}

if (!actbcc.equals("-"))
{
actbcc = actbcc.substring(0,actbcc.length()-1);
bcc = InternetAddress.parse(actbcc);
mimemessage.setRecipients(Message.RecipientType.BCC, bcc);
transport.sendMessage(mimemessage, bcc);
}

out.println("




");
out.println("
");
out.println("");
out.println("

");
out.println("Your Message has been successfully posted to the specified users.");
out.println("

Back

");

}
catch(Exception _ex)
{
out.println("




");
out.println("
");
out.println("");
out.println("

");
out.println("Problem in submitting the form.");
out.println("

Back

");
}
%>

Re: sending a javamail

Posted By:   julien_cool  
Posted On:   Wednesday, February 27, 2002 12:50 AM

there is a complete beginner tutorial on this forum/faq or at java.sun.com
About | Sitemap | Contact