Java 邮件发送代码详解编程语言

发送邮件

1、MailOperation.java 主要文件,邮件操作,发送
package main; 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.Multipart; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
public class MailOperation { 
/** 
* @author goming 
*TODO:发送邮件 
[email protected] user 
[email protected] password 
[email protected] host 
[email protected] from 
[email protected] to 
[email protected] subject 
[email protected] content 
[email protected] 
[email protected] Exception 
*/ 
public String sendMail(String   user,String password,String host,String from,String to,String subject,String   content) throws Exception { 
// TODO Auto-generated method stub 
if(to!=null) 
{ 
Properties props=System.getProperties(); 
props.put("mail.smtp.host", host); 
props.put("mail.smtp.auth", "true");  
MailAuthenticator auth=new   MailAuthenticator(); 
MailAuthenticator.USERNAME=user; 
MailAuthenticator.PASSWORD=password; 
Session session=Session.getInstance(props,auth); 
session.setDebug(true); 
try{ 
MimeMessage message=new   MimeMessage(session); 
//message.setDataHandler(new DataHandler(content, "text/html;   charset=utf-8"));//设置邮件内容 
message.setFrom(new   InternetAddress(from)); 
if(!to.trim().equals("")) 
message.addRecipient(Message.RecipientType.TO, new   InternetAddress(to.trim())); 
message.setSubject(subject); 
//  message.setContent(mp) 
//message.setText("this is 一个测试邮件"); 
MimeBodyPart mbp1=new   MimeBodyPart();  //正文 
mbp1.setContent(content,"text/html;charset=utf-8"); 
Multipart mp=new MimeMultipart();   //整个邮件:正文+附件 
mp.addBodyPart(mbp1); 
//mp.addBodyPart(mbp2); 
message.setContent(mp); 
message.setSentDate(new Date()); 
message.saveChanges(); 
Transport   trans=session.getTransport("smtp"); 
trans.send(message); 
System.out.println(message.toString()); 
}catch(Exception e) 
{ 
e.printStackTrace(); 
return "failure"; 
} 
return "success"; 
} 
else 
return "failure"; 
} 
public static void main(String[] args) { 
MailOperation operation = new MailOperation(); 
String user = "[email protected]"; 
String password = "xmG0ldenway"; 
String host = "smtp.qq.com"; 
String from = "[email protected]"; 
String to = "[email protected]";//收件人 
String subject = "Test"; 
String content = "测试邮件"; 
try { 
String res = operation.sendMail(user,   password, host, from, to, subject, content); 
System.out.println(res); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println(new Date()); 
} 
}

 

2、MailAuthenticator.jave 封装发件人账号密码
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
/** 
* 
*   @author goming 
* 
*/ 
public class MailAuthenticator extends   Authenticator 
{ 
public   static String USERNAME = ""; 
public   static String PASSWORD = ""; 
public   MailAuthenticator(){} 
protected   PasswordAuthentication getPasswordAuthentication(){ 
return   new PasswordAuthentication(USERNAME, PASSWORD); 
} 
}

 

3、SendEmail.java 调用类,主要做邮件内容,以及发送一个地址回访!
/** 
* 发送邮件 
* @author goming 
* @param to 
* @param uuid 
*/ 
@RequestMapping(params = "sendMail") 
@ResponseBody 
private void sendMail(String   to,HttpServletRequest request){ 
MailOperation operation = new MailOperation(); 
//发件人 
String user = "[email protected]"; 
String password = "******"; 
String host = "smtp.qq.com"; 
String from = "[email protected]"; 
String subject = "感谢您注册创意生态资源集成系统软件"; 
//邮箱内容 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; 
StringBuffer sb = new StringBuffer(); 
Date date=new Date(); 
Long longtimeLong=date.getTime(); 
sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa'   style='border:1px solid #d9f4ee; font-size:12px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px;   padding-bottom:5px;'><span style='font-weight:bold;'>安全提示:" 
+ "<div style='width:950px;font-family:arial;'>为了您的账户安全,我们建议您点击以下链接验证邮箱:<br/><a href='"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"'>"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"</a><br/>请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。<br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>厦门交叉媒体科技有限公司</div>" 
+"</div>"); 
try { 
String res = operation.sendMail(user,   password, host, from, to, subject, sb.toString()); 
//         System.out.println(res); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10362.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论