以前整理的一段代碼,來源于網上,大家可以使用它連接到普通的郵件服務器或gmail郵件服務器發送郵件通知等.
現在還只是發送文本郵件,有興趣的可以再看一下如何發送超文本郵件,帶附件等.
//Author:Adrian 20060707
package asdf.asdf.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
//使用gmail的郵件服務器
public ?class EmailNoticeEx{
? ?public static String sendSSLGmail(String fromName,String
fromEmail,String smtpServer,String smtpUser,String smtpPassword,String
recipient_name,String recipients[],String subject,String message,String
encoding){
? try{
? ?boolean debug = false;
? ?java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
? ?// Set the host smtp address
? ?// ?設置系統屬性
? ?Properties props = new Properties();//獲得系統屬性對象
? ?props.put("mail.transport.protocol", "smtp"); ? ?
? ?props.put("mail.host", smtpServer);//設置SMTP主機
? ?MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
? ?//獲得郵件會話對象
? ?Session mailSession = Session.getDefaultInstance(props, auth);
? ?mailSession.setDebug(debug);
? ?javax.mail.internet.MimeMessage mimeMsg; //MIME郵件對象
? ?
? ?//創建MIME郵件對象
? ?mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
? ?// create a message
? ?//Message msg = new MimeMessage(session);
? ?// set the from and to address
? ?InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
? ?mimeMsg.setFrom(addressFrom);
? ?InternetAddress[] addressTo = new InternetAddress[recipients.length];
? ?for (int i = 0; i < recipients.length; i++) {
? ? ? ?addressTo[i] = new InternetAddress(recipients[i]);
? ?}
? ?mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
? ?// Setting the Subject and Content Type
? ?mimeMsg.setSubject(subject,encoding);
? ?mimeMsg.setContent(message, "text/plain");
? ?mimeMsg.setText(message,encoding);
? ?Transport.send(mimeMsg);
? }catch(Exception e){
? ? ? e.printStackTrace();
? ? ? return e.getMessage();
? }
? return null;
?}
//使用普通的郵件服務器 ?
? ?public static String sendMail(String fromName,String
fromEmail,String smtpServer,String smtpUser,String smtpPassword,String
recipient_name,String recipients[],String subject,String message,String
encoding){
? ? ? ? ? try{
? ? ? ? ? ?boolean debug = false;
? ? ? ? ? ?// Set the host smtp address
? ? ? ? ? ?// ?設置系統屬性
? ? ? ? ? ?Properties props = new Properties();//獲得系統屬性對象
? ? ? ? ? ?props.put("mail.transport.protocol", "smtp");
? ? ? ? ? ?props.put("mail.smtp.starttls.enable","true");
? ? ? ? ? ?props.put("mail.smtp.host", smtpServer);//設置SMTP主機
? ? ? ? ? ?props.put("mail.smtp.auth", "true");
? ? ? ? ? ?
? ? ? ? ? ?MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
? ? ? ? ? ?//獲得郵件會話對象
? ? ? ? ? ?Session mailSession = Session.getDefaultInstance(props, auth);
? ? ? ? ? ?mailSession.setDebug(debug);
? ? ? ? ? ?javax.mail.internet.MimeMessage mimeMsg; //MIME郵件對象
? ? ? ? ? ?
? ? ? ? ? ?//創建MIME郵件對象
? ? ? ? ? ?mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
? ? ? ? ? ?// create a message
? ? ? ? ? ?//Message msg = new MimeMessage(session);
? ? ? ? ? ?// set the from and to address
? ? ? ? ? ?InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
? ? ? ? ? ?mimeMsg.setFrom(addressFrom);
? ? ? ? ? ?InternetAddress[] addressTo = new InternetAddress[recipients.length];
? ? ? ? ? ?for (int i = 0; i < recipients.length; i++) {
? ? ? ? ? ? ? ?addressTo[i] = new InternetAddress(recipients[i]);
? ? ? ? ? ?}
? ? ? ? ? ?mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
? ? ? ? ? ?// Setting the Subject and Content Type
? ? ? ? ? ?mimeMsg.setSubject(subject,encoding);
? ? ? ? ? ?mimeMsg.setContent(message, "text/plain");
? ? ? ? ? ?mimeMsg.setText(message,encoding);
? ? ? ? ? ?Transport.send(mimeMsg);
? ? ? ? ? }catch(Exception e){
? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? return e.getMessage();
? ? ? ? ? }
? ? ? ? ? return null;
? ? ? ? ?}
? ? ? ? ? ?
?
}// end class
// smtp需要驗證時候的驗證類
class MyAuthenticator
? ?extends javax.mail.Authenticator {
?private String strUser;
?private String strPwd;
?public MyAuthenticator(String user, String password) {
? ?this.strUser = user;
? ?this.strPwd = password;
?}
?protected PasswordAuthentication getPasswordAuthentication() {
? ?return new PasswordAuthentication(strUser, strPwd);
?}
}
樓主:
navIme2
plpl!gjgj!