• ADADADADAD

    如何用SSL加密邮件[ 网络知识 ]

    网络知识 时间:2024-11-20 12:07:52 热度:3℃

    作者:文/会员上传 下载docx

    简介:

    使用SSL加密邮件的示例:例如在JavaMail中使用SSL对邮件发送进行加密,源码如下:packagejavamail.yisu.com;importjava.io.UnsupportedEncodingException;importjava.util.ArrayL

    以下为本文的正文内容,请查阅,本站为公益性网站,复制本文以及下载DOC文档全部免费。

    使用SSL加密邮件的示例:

    例如在JavaMail中使用SSL对邮件发送进行加密,源码如下:

    packagejavamail.yisu.com;

    importjava.io.UnsupportedEncodingException;

    importjava.util.ArrayList;

    importjava.util.Date;

    importjava.util.List;

    importjava.util.Properties;

    importjavax.activation.DataHandler;

    importjavax.activation.FileDataSource;

    importjavax.mail.Address;

    importjavax.mail.BodyPart;

    importjavax.mail.Multipart;

    importjavax.mail.Session;

    importjavax.mail.Transport;

    importjavax.mail.internet.InternetAddress;

    importjavax.mail.internet.MimeBodyPart;

    importjavax.mail.internet.MimeMessage;

    importjavax.mail.internet.MimeMultipart;

    importjavax.mail.internet.MimeUtility;

    publicclassSendMailBySSL{

    privatefinalStringSSL_FACTORY="javax.net.ssl.SSLSocketFactory";

    privateStringsmtpServer;//SMTP服务器地址

    privateStringport;//端口

    privateStringusername;//登录SMTP服务器的用户名

    privateStringpassword;//登录SMTP服务器的密码

    privateListrecipients=newArrayList();//收件人地址集合

    privateStringsubject;//邮件主题

    privateStringcontent;//邮件正文

    privateListp_w_uploadNames=newArrayList();//附件路径信息集合

    publicSendMailBySSL(){

    }

    publicSendMailBySSL(StringsmtpServer,Stringport,Stringusername,

    Stringpassword,Listrecipients,Stringsubject,

    Stringcontent,Listp_w_uploadNames){

    this.smtpServer=smtpServer;

    this.port=port;

    this.username=username;

    this.password=password;

    this.recipients=recipients;

    this.subject=subject;

    this.content=content;

    this.p_w_uploadNames=p_w_uploadNames;

    }

    publicvoidsetSmtpServer(StringsmtpServer){

    this.smtpServer=smtpServer;

    }

    publicvoidsetPort(Stringport){

    this.port=port;

    }

    publicvoidsetUsername(Stringusername){

    this.username=username;

    }

    publicvoidsetPassword(Stringpassword){

    this.password=password;

    }

    publicvoidsetRecipients(Listrecipients){

    this.recipients=recipients;

    }

    publicvoidsetSubject(Stringsubject){

    this.subject=subject;

    }

    publicvoidsetContent(Stringcontent){

    this.content=content;

    }

    publicvoidsetAttachmentNames(Listp_w_uploadNames){

    this.p_w_uploadNames=p_w_uploadNames;

    }

    /**

    *进行base64加密,防止中文乱码

    **/

    publicStringchangeEncode(Stringstr){

    try{

    str=MimeUtility.encodeText(newString(str.getBytes(),"UTF-8"),

    "UTF-8","B");//"B"代表Base64

    }catch(UnsupportedEncodingExceptione){

    e.printStackTrace();

    }

    returnstr;

    }

    /**

    *正式发邮件

    **/

    publicbooleansendMail(){

    Propertiesproperties=newProperties();

    properties.put("mail.smtp.host",smtpServer);

    properties.put("mail.smtp.auth","true");

    properties.put("mail.smtp.socketFactory.class",SSL_FACTORY);//使用JSSE的SSLsocketfactory来取代默认的socketfactory

    properties.put("mail.smtp.socketFactory.fallback","false");//只处理SSL的连接,对于非SSL的连接不做处理

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

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

    Sessionsession=Session.getInstance(properties);

    session.setDebug(true);

    MimeMessagemessage=newMimeMessage(session);

    try{

    //发件人

    Addressaddress=newInternetAddress(username);

    message.setFrom(address);

    //收件人

    for(Stringrecipient:recipients){

    System.out.println("收件人:"+recipient);

    AddresstoAddress=newInternetAddress(recipient);

    message.setRecipient(MimeMessage.RecipientType.TO,toAddress);//设置收件人,并设置其接收类型为TO

    /**

    *TO:代表有健的主要接收者。CC:代表有健的抄送接收者。BCC:代表邮件的暗送接收者。

    **/

    }

    //主题

    message.setSubject(changeEncode(subject));

    //时间

    message.setSentDate(newDate());

    Multipartmultipart=newMimeMultipart();

    //添加文本

    BodyParttext=newMimeBodyPart();

    text.setText(content);

    multipart.addBodyPart(text);

    //添加附件

    for(StringfileName:p_w_uploadNames){

    BodyPartadjunct=newMimeBodyPart();

    FileDataSourcefileDataSource=newFileDataSource(fileName);

    adjunct.setDataHandler(newDataHandler(fileDataSource));

    adjunct.setFileName(changeEncode(fileDataSource.getName()));

    multipart.addBodyPart(adjunct);

    }

    //清空收件人集合,附件集合

    recipients.clear();

    p_w_uploadNames.clear();

    message.setContent(multipart);

    message.saveChanges();

    }catch(Exceptione){

    e.printStackTrace();

    returnfalse;

    }

    try{

    Transporttransport=session.getTransport("smtp");

    transport.connect(smtpServer,username,password);

    transport.sendMessage(message,message.getAllRecipients());

    transport.close();

    }catch(Exceptione){

    e.printStackTrace();

    returnfalse;

    }

    returntrue;

    }

    publicstaticvoidmain(String[]args){

    Listrecipients=newArrayList();

    //recipients.add("123456789@qq.com");

    recipients.add("admin@yisu.cn");

    Stringsubject="这封邮件是为了测试SMTP的SSL加密传输";

    Stringcontent="这是这封邮件的正文";

    Listp_w_uploadNames=newArrayList();

    p_w_uploadNames.add("C://Users//Administrator//Desktop//kali.txt");

    SendMailBySSLsendMailBySSL=newSendMailBySSL("smtp.163.com","465",

    "youname@163.com","youpassword",recipients,subject,content,

    p_w_uploadNames);

    sendMailBySSL.sendMail();

    }

    }

    如何用SSL加密邮件.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: ssl
    ADADAD
    热门栏目