java mail ssl smtp_使用JavaMail发送邮件,465端口开启ssl加密传输

packagecom.wangxin.test;importjava.security.Security;importjava.util.Date;importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;/*** javaMail的邮件工具类

*@authorwangXgnaw

**/

public classMailUtil {/*** 使用加密的方式,利用465端口进行传输邮件,开启ssl

*@paramto 为收件人邮箱

*@parammessage 发送的消息*/

public static voidsendEmil(String to, String message) {try{

Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";//设置邮件会话参数

Properties props = newProperties();//邮箱的发送服务器地址

props.setProperty("mail.smtp.host", "smtp.sina.com");

props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);

props.setProperty("mail.smtp.socketFactory.fallback", "false");//邮箱发送服务器端口,这里设置为465端口

props.setProperty("mail.smtp.port", "465");

props.setProperty("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.auth", "true");final String username = "发送者邮箱用户名";final String password = "发送者邮箱密码或者邮箱授权码";//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm

Session session = Session.getDefaultInstance(props, newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(username, password);

}

});//通过会话,得到一个邮件,用于发送

Message msg = newMimeMessage(session);//设置发件人

msg.setFrom(new InternetAddress("发件人邮箱"));//设置收件人,to为收件人,cc为抄送,bcc为密送

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to,false));

msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to,false));

msg.setSubject("邮件主题");//设置邮件消息

msg.setText(message);//设置发送的日期

msg.setSentDate(newDate());//调用Transport的send方法去发送邮件

Transport.send(msg);

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于JavaMail库的简单的SMTP发送邮件程序,使用465端口进行加密传输: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SMTPClient { public static void main(String[] args) { final String username = "your_email@example.com"; final String password = "your_password"; final String recipient = "recipient_email@example.com"; final String subject = "Test Email"; final String body = "This is a test email sent using JavaMail API."; Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject(subject); message.setText(body); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 其中,需要修改的部分包括: - `username`:发送邮件的邮箱账号 - `password`:发送邮件的邮箱密码 - `recipient`:接收邮件的邮箱地址 - `props.put("mail.smtp.host", "smtp.example.com")`:SMTP服务器的地址,需要根据使用的邮箱服务进行修改 其他部分可根据需要进行调整。注意,此程序需要引入JavaMail库的相关依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值