邮件权限的开通以及java后台代码

 1、首先对发件人的邮箱开启 SMIT 服务器以及获取授权码,开启历程参考百度
 2、下载formail,邮箱地址以及授权码进行首次登录,
 3、设置SMIT服务器账号和密码

图片 

服务器是自动生成,如果没有生成手动输入

图片 
设置好之后保存,之后就开启了服务器
4、后台代码,可复制直接用
package  com.jf.test ;


import  java.io.UnsupportedEncodingException ;
import  java.util.ArrayList ;
import  java.util.Date ;
import  java.util.List ;
import  java.util.Properties ;

import  javax.activation.DataHandler ;
import  javax.activation.FileDataSource ;
import  javax.mail.Address ;
import  javax.mail.BodyPart ;
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 ;
import  javax.mail.internet.MimeUtility ;

public class  SendMailBySSL {
private final  String  SSL_FACTORY  "javax.net.ssl.SSLSocketFactory" ;
private  String  smtpServer // SMTP 服务器地址
private  String  port //  端口
private  String  username //  登录 SMTP 服务器的用户名
private  String  password //  登录 SMTP 服务器的密码
private  List<String>  recipients  new  ArrayList<String>() //  收件人地址集合
private  String  subject //  邮件主题
private  String  content //  邮件正文
private  List<String>  attachmentNames  new  ArrayList<String>() //  附件路径信息集合

public  SendMailBySSL () {

}

public  SendMailBySSL (String smtpServer String port String username ,
String password List<String> recipients String subject ,
String content List<String> attachmentNames) {
this . smtpServer  = smtpServer ;
this . port  = port ;
this . username  = username ;
this . password  = password ;
this . recipients  = recipients ;
this . subject  = subject ;
this . content  = content ;
this . attachmentNames  = attachmentNames ;
}

public void  setSmtpServer (String smtpServer) {
this . smtpServer  = smtpServer ;
}

public void  setPort (String port) {
this . port  = port ;
}

public void  setUsername (String username) {
this . username  = username ;
}

public void  setPassword (String password) {
this . password  = password ;
}

public void  setRecipients (List<String> recipients) {
this . recipients  = recipients ;
}

public void  setSubject (String subject) {
this . subject  = subject ;
}

public void  setContent (String content) {
this . content  = content ;
}

public void  setAttachmentNames (List<String> attachmentNames) {
this . attachmentNames  = attachmentNames ;
}

/**
进行 base64 加密,防止中文乱码
* */
public  String  changeEncode (String str) {
try  {
str = MimeUtility. encodeText ( new  String(str.getBytes() "UTF-8" ) ,
"UTF-8" "B" ) // "B" 代表 Base64
catch  (UnsupportedEncodingException e) {
e.printStackTrace() ;
}
return  str ;
}

/**
正式发邮件
* */
public boolean  sendMail () {
Properties properties =  new  Properties() ;
properties.put( "mail.smtp.host" smtpServer ) ;
properties.put( "mail.smtp.auth" "true" ) ;
properties.put( "mail.smtp.socketFactory.class" SSL_FACTORY ) // 使用 JSSE SSL socketfactory 来取代默认的 socketfactory
properties.put( "mail.smtp.socketFactory.fallback" "false" ) //  只处理 SSL 的连接 , 对于非 SSL 的连接不做处理

properties.put( "mail.smtp.port" port ) ;
properties.put( "mail.smtp.socketFactory.port" port ) ;

Session session = Session. getInstance (properties) ;
session.setDebug( true ) ;
MimeMessage message =  new  MimeMessage(session) ;

try  {
//  发件人
Address address =  new  InternetAddress( username ) ;
message.setFrom(address) ;

//  收件人
for  (String recipient :  recipients ) {
System. out .println( " 收件人: + recipient) ;
Address toAddress =  new  InternetAddress(recipient) ;
message.setRecipient(MimeMessage.RecipientType. TO toAddress) //  设置收件人 , 并设置其接收类型为 TO
/**
* TO :代表有健的主要接收者。  CC :代表有健的抄送接收者。  BCC :代表邮件的暗送接收者。
* */
}

//  主题
message.setSubject(changeEncode( subject )) ;

//  时间
message.setSentDate( new  Date()) ;

Multipart multipart =  new  MimeMultipart() ;
//  添加文本
BodyPart text =  new  MimeBodyPart() ;
text.setText( content ) ;
multipart.addBodyPart(text) ;
//  添加附件
for  (String fileName :  attachmentNames ) {
BodyPart adjunct =  new  MimeBodyPart() ;
FileDataSource fileDataSource =  new  FileDataSource(fileName) ;
adjunct.setDataHandler( new  DataHandler(fileDataSource)) ;
adjunct.setFileName(changeEncode(fileDataSource.getName())) ;
multipart.addBodyPart(adjunct) ;
}
//  清空收件人集合,附件集合
recipients .clear() ;
attachmentNames .clear() ;

message.setContent(multipart) ;
message.saveChanges() ;

catch  (Exception e) {
e.printStackTrace() ;
return false;
}

try  {
Transport transport = session.getTransport( "smtp" ) ;
transport.connect( smtpServer username password ) ;
transport.sendMessage(message message.getAllRecipients()) ;
transport.close() ;
catch  (Exception e) {
e.printStackTrace() ;
return false;
}

return true;
}

public static void  main (String[] args) {
List<String> recipients =  new  ArrayList<String>() ;
recipients.add( "1836735020@qq.com" ) ;
// recipients.add("admin@zifangsky.cn");
String subject =  " 这封邮件是为了测试 SMTP SSL 加密传输 " ;
String content =  " 这是这封邮件的正文 " ;
List<String> attachmentNames =  new  ArrayList<String>() ;
// 添加附件

// attachmentNames.add("C://Users//huahua.txt");

//775055015@qq.com为原发送邮件的邮箱,"itsmvlhlxxlsbfei"为开通发送邮件功能权限的时候生成的授权码

SendMailBySSL sendMailBySSL =  new  SendMailBySSL( "smtp.qq.com" "465" ,
"775055015@qq.com" "itsmvlhlxxlsbfei" recipients subject content ,
attachmentNames) ;
sendMailBySSL.sendMail() ;
}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值