private String from = null; private String subject = null; private String content = null; private String to = null; private String cc = null; private String bcc = null; private String smtpHost = new String("mail.bcstnet.com"); private String smtpPort = new String("25"); private String userName = new String("asdasdf"); private String password = new String("adfasdfa");
private boolean debug = true;
public static void main (String[] args) { SendMail mail = new SendMail (); mail.setFrom (""); mail.setSubject ("only for test java SendMail"); //mail.setTo ("[email protected]"); mail.setTo ("[email protected]"); mail.setContent ("this is only a test"); mail.send (); } public SendMail () { }
package creatxr.demos.struts.services.mail;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Transport;import javax.mail.PasswordAuthentication;
import creatxr.demos.struts.services.mail.PopAuthenticator;public class SendMail {
private String from = null;
private String subject = null;
private String content = null;
private String to = null;
private String cc = null;
private String bcc = null;
private String smtpHost = new String("mail.bcstnet.com");
private String smtpPort = new String("25");
private String userName = new String("asdasdf");
private String password = new String("adfasdfa");
private boolean debug = true;
public static void main (String[] args) {
SendMail mail = new SendMail ();
mail.setFrom ("");
mail.setSubject ("only for test java SendMail");
//mail.setTo ("[email protected]");
mail.setTo ("[email protected]");
mail.setContent ("this is only a test");
mail.send ();
} public SendMail () {
}
public void send () {
Properties props = System.getProperties ();
props.put ("mail.smtp.host", this.smtpPort);
props.put ("mail.smtp.auth", "true");
PopAuthenticator popAuthenticator = new PopAuthenticator ();
PasswordAuthentication pop = popAuthenticator.performCheck (this.userName, this.password);
//Session mailSession = Session.getInstance (props, null);
Session mailSession = Session.getInstance (props, popAuthenticator);
mailSession.setDebug (debug);
try {
MimeMessage msg = new MimeMessage (mailSession);
msg.setFrom (new InternetAddress(this.from));
msg.setSubject (this.subject);
InternetAddress[] toAddress = {new InternetAddress(this.to)};
msg.setRecipients (Message.RecipientType.TO, toAddress);
MimeBodyPart contentPart = new MimeBodyPart ();
contentPart.setText (this.content);
//MimeBodyPart attachmentPart = new MimeBodyPart ();
Multipart multipart = new MimeMultipart ();
multipart.addBodyPart (contentPart);
msg.setContent (multipart);
Transport.send (msg);
} catch (Exception e) {
} finally {
}
System.out.println ("success send");
}
public void setTo (String to) {
this.to = to;
}
public void setCc (String cc) {
this.cc = cc;
}
public void setBcc (String bcc) {
this.bcc = bcc;
}
public void setFrom (String from) {
this.from = from;
}
public void setSubject (String subject) {
this.subject = subject;
}
public void setContent (String content) {
this.content = content;
}
public void addAttechment (String fileName) {
}
}
Properties props = System.getProperties ();
props.put ("mail.transport.protocol", "smtp");
props.put ("mail.smtp.host", this.smtpHost);
props.put ("mail.smtp.port", this.smtpPort);
props.put ("mail.smtp.auth", "true");