标签:vax style htm top smtp imp turn stat protocol
import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail { /** *
String uto="2506715686@qq.com";//获取收件人邮箱
String topic="系统报错";//发送主题
String content="error";//发送内容
String sendemail="sys857@163.com";
String sendname="终结者系统";
String connecthost="smtp.163.com";
String connectemailname="sys857";
String connectemailpass="cyunlnvuctkqwtvs";
* @param uto 获取收件人邮箱 * @param topic 发送主题 * @param content 发送内容 * @param sendemail 发送人的邮箱 * @param sendname 发送人姓名 * @param connecthost 邮箱主机 * @param connectemailname 邮箱用户名 * @param connectemailpass 邮箱发送密钥(不是登录密码) * @return boolean */ public static boolean sendemail(String uto,String topic,String content, String sendemail,String sendname,String connecthost, String connectemailname,String connectemailpass,Integer port) { //验证邮箱格式开始 boolean flag=true; String str="^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern regex=Pattern.compile(str); Matcher matcher=regex.matcher(uto); flag=matcher.matches(); //邮箱验证 try { if(flag){ //设置协议 Properties pros = new Properties(); pros.put("mail.transport.protocol","smtp"); pros.put("mail.smtp.auth", true); pros.put("mail.smtp.port", port); // qq pros.setProperty("mail.debug", "false"); //Authenticator authenticator=new Authenticator(); Session session=Session.getInstance(pros); session.setDebug(true); //设置发送的邮件 MimeMessage msg = new MimeMessage(session); //设置发送人的邮箱和姓名 Address toaddress=new InternetAddress(sendemail,sendname); msg.setFrom(toaddress); // /* Address[] cc={new InternetAddress(""),new InternetAddress("")}; msg.setRecipients(Message.RecipientType.CC, cc); */ msg.setRecipients(Message.RecipientType.TO, uto);//设置收件人 msg.setSubject(topic);//主题 msg.setText(content);//内容 msg.setContent(content,"text/html;charset=utf-8");//发送html格式的邮件 msg.saveChanges();//保存发送 //取得发送工具 Transport ts=session.getTransport(); //设置连接 邮箱主机 邮箱用户名 邮箱密码 ts.connect(connecthost,connectemailname,connectemailpass); ts.sendMessage(msg, msg.getAllRecipients());//发送 ts.close();//关闭 return true; }else{ return false; } }catch(Exception e){ return false; } } }
标签:vax style htm top smtp imp turn stat protocol
原文地址:http://www.cnblogs.com/syscn/p/7742294.html