import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* @param args
*/
public void postMail( String[] recipients, String subject, String message , String from) throws MessagingException
{
boolean debug = false;
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.ddd.com");
props.put("mail.smtp.auth","true");
// create some properties and get the default Session
MyAuthenticator auth = new MyAuthenticator("ddd","dd6");
// MyAuthenticator auth = new MyAuthenticator("msmp","123456");
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
MimeMessage msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
// InternetAddress[] addressTo = new InternetAddress[recipients.length];
// for (int i = 0; i < recipients.length; i++)
// {
// addressTo[i] = new InternetAddress(recipients[i]);
// }
// msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
// msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
Address[] to = new InternetAddress[] {
new InternetAddress("")
};
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
// Transport tarnsport = session.getTransport("smtp");
// tarnsport.sendMessage(msg, msg.getAllRecipients());
// tarnsport.close();
Transport.send(msg);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SendMail send = new SendMail();
String[] recipients = {""};
try {
send.postMail(recipients, "mao test", "maojj test smsmp", "");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
String username, password;
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
阅读(917) | 评论(0) | 转发(0) |