javamail send

package org.jixiuf.javamail2; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Test2 { String host = ""; String user = ""; String password = ""; public void setHost(String host) { this.host = host; } public void setAccount(String user, String password) { this.user = user; this.password = password; } public void send(String from, String to, String subject, String content) { Properties props = new Properties(); props.put("mail.smtp.host", host);// 指定SMTP服务器 props.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证 try { Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(true);// 是否在控制台显示debug信息 Message message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(from));// 发件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));// 收件人 message.setSubject(subject);// 邮件主题 message.setText(content);// 邮件内容 message.saveChanges(); Transport transport = mailSession.getTransport("smtp"); transport.connect(host, user, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { System.out.println(e); } } public static void main(String args[]) { Test2 sm = new Test2(); sm.setHost("58.251.149.147");// 指定要使用的邮件服务器 sm.setAccount("jixiuf", "zhao21_");// 指定帐号和密码 sm.send("[email protected]", "[email protected]", "title", "new java"); } }

这里有源代码,并且对发送进行了一定的封装,就是在以上代码的基础上


http://download.csdn.net/source/2673139

你可能感兴趣的:(exception,String,properties,user,javamail,邮件服务器)