Java发送邮件实例
?? 发纯文本的邮件??????????
?
???????????? 首先下载commons-email-1.1.jar、mail.jar和activation.jar(它们可以从sun的网站上下载,从下载的javamail和jaf中找到)。
???????? (一)使用javax.mail.*
?
import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; public class Send { private String SMTPHost="";//SMTP服务器private String user="";//登录SMTP服务器的账号private String passwd;private String from="";//发件人邮箱private String to="";//收件人邮箱private String subject="";//邮件标题private String content="";//邮件内容 public Send(String host, String user, String passwd,String to, String subject, String content) {SMTPHost = host;this.user = user;this.passwd = passwd;this.from = user;this.to = to;this.subject = subject;this.content = content;}public static void main(String[] args) { Send send=new Send("smtp.163.com","****@163.com","******","**email_address**","",""); send.setSubject("注册成功!!!"); send.setContent("您好!!!您的注册已通过审核请注意查看!!!"); send.send(); } public void send() { SimpleEmail email = new SimpleEmail(); email.setTLS(true); email.setHostName(this.SMTPHost); email.setAuthentication(this.user, this.passwd); try { //to email.addTo(this.to); //from email.setFrom(this.from); //subject email.setSubject(this.subject); //标题 //setting Charset email.setCharset("UTF-8"); //content email.setMsg(this.content); //send email.send(); System.out.println("发送完毕"); } catch (EmailException e) { e.printStackTrace(); } }/** * @return the sMTPHost */public String getSMTPHost() {return SMTPHost;}/** * @param host the sMTPHost to set */public void setSMTPHost(String host) {SMTPHost = host;}/** * @return the user */public String getUser() {return user;}/** * @param user the user to set */public void setUser(String user) {this.user = user;}/** * @return the passwd */public String getPasswd() {return passwd;}/** * @param passwd the passwd to set */public void setPasswd(String passwd) {this.passwd = passwd;}/** * @return the from */public String getFrom() {return from;}/** * @param from the from to set */public void setFrom(String from) {this.from = from;}/** * @return the to */public String getTo() {return to;}/** * @param to the to to set */public void setTo(String to) {this.to = to;}/** * @return the subject */public String getSubject() {return subject;}/** * @param subject the subject to set */public void setSubject(String subject) {this.subject = subject;}/** * @return the content */public String getContent() {return content;}/** * @param content the content to set */public void setContent(String content) {this.content = content;} }
?
另外还有发送html的邮件和包含附件的邮件,在此只谈一下发纯文本的邮件