package com.in.smtp;
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class EmailForSmtp {
String host = "";
String username = "";
String password = "";
public void setHost(String host) {
this.host = host;
}
public void setUserInformaition(String username,String password) {
this.username = username;
this.password = password;
}
public void sendEmail(String sendpeople,String arrviedpeople,String emailname,String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", true);
try {
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(sendpeople));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(arrviedpeople));//收件人
msg.setSubject(emailname);//邮件主题
msg.setText(content);//邮件内容
msg.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------
Client
import com.in.smtp.EmailForSmtp;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EmailForSmtp efs = new EmailForSmtp();
efs.setHost("smtp.163.com");
efs.setUserInformaition("manytao", "wangtao521");
efs.sendEmail("
}
ssss@163.com", "bbb_bbb@163.com", "aaaa", "bbbbb");
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/manytao/archive/2010/10/03/5920080.aspx