自制简单发邮件jar包

来源:互联网 发布:盗贼源码解压密码 编辑:程序博客网 时间:2024/06/11 07:27

我制作的发邮件的包

<pre name="code" class="java">import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;public class Xmail {/** *  */public String username;public String password;public String to;public String from;public String subject;public String content;public String host;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getHost() {return host;}public void setHost(String host) {this.host = host;}public void sendMail() throws AddressException, MessagingException {Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.host", getHost());props.put("mail.smtp.port", "25");Session session = Session.getDefaultInstance(props,new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);};});MimeMessage mailMessage = new MimeMessage(session);mailMessage.setFrom(new InternetAddress(getUsername()));// Message.RecipientType.TO属性表示接收者的类型为TOmailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(getTo()));mailMessage.setSubject(getSubject(), "UTF-8");mailMessage.setSentDate(new Date());// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象Multipart mainPart = new MimeMultipart();// 创建一个包含HTML内容的MimeBodyPartBodyPart html = new MimeBodyPart();html.setContent(getContent(), "text/html; charset=utf-8");mainPart.addBodyPart(html);mailMessage.setContent(mainPart);Transport.send(mailMessage);System.out.println("send mail success");}}



实例:

package wx;import javax.mail.MessagingException;import javax.mail.internet.AddressException;public class test {public static void main(String[] args) throws AddressException,MessagingException {Xmail xmail = new Xmail();xmail.setUsername("/////@163.com");   //设置发件人邮箱名字xmail.setPassword("*******");     //邮箱密码xmail.setHost("smtp.163.com");          //邮箱服务器  163邮箱为"smtp.163.com",qq邮箱为"smtp.qq.com"xmail.setSubject("dfsfs");                 //设置邮件主题xmail.setContent("dsfas");                  //设置邮件内容xmail.setTo("12345678@qq.com");          //设置邮件收件人,任意邮箱xmail.sendMail();}}


0 0
原创粉丝点击