java mail urlname_javamail发件、收件(SSL连接)

importjava.net.URL;importjava.security.Security;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;//发送一封图文加附件的邮件

public classSendMail {public static voidmain(String[] args) {try{//设置SSL连接、邮件环境

Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

Properties props=System.getProperties();

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

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

props.setProperty("mail.smtp.socketFactory.fallback", "false");

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

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

props.setProperty("mail.smtp.auth", "true");//props.put("mail.smtp.host", "smtp.qq.com");//props.put("mail.smtp.socketFactory.class", SSL_FACTORY);//props.put("mail.smtp.socketFactory.fallback", "false");//props.put("mail.smtp.port", "465");//props.put("mail.smtp.socketFactory.port", "465");//props.put("mail.smtp.auth", "true");//建立邮件会话

Session session = Session.getDefaultInstance(props, newAuthenticator() {//身份认证

protectedPasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");

}

});//建立邮件对象

MimeMessage message = newMimeMessage(session);//设置邮件的发件人、收件人、主题//附带发件人名字//message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));

message.setFrom(new InternetAddress("from_mail@qq.com"));

message.setRecipients(Message.RecipientType.TO,"to_mail@qq.com");

message.setSubject("通过javamail发出!!!");//文本部分

MimeBodyPart textPart = newMimeBodyPart();

textPart.setContent("图%E2%80%98cid:myimg%E2%80%98文加附件邮件测试", "text/html;charset=UTF-8");//内嵌图片部分

MimeBodyPart imagePart = newMimeBodyPart();

imagePart.setDataHandler(new DataHandler(new FileDataSource("imagePath")));//图片路径

imagePart.setContentID("myimg");//图文整合,关联关系

MimeMultipart mmp1 = newMimeMultipart();

mmp1.addBodyPart(textPart);

mmp1.addBodyPart(imagePart);

mmp1.setSubType("related");

MimeBodyPart textImagePart= newMimeBodyPart();

textImagePart.setContent(mmp1);//附件部分

MimeBodyPart attachmentPart = newMimeBodyPart();

DataHandler dh= new DataHandler(new FileDataSource("filePath"));//文件路径

String fileName =dh.getName();

attachmentPart.setDataHandler(dh);

attachmentPart.setFileName(fileName);//图文和附件整合,复杂关系

MimeMultipart mmp2 = newMimeMultipart();

mmp2.addBodyPart(textImagePart);

mmp2.addBodyPart(attachmentPart);

mmp2.setSubType("mixed");//将以上内容添加到邮件的内容中并确认

message.setContent(mmp2);

message.saveChanges();//发送邮件

Transport.send(message);

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaMail 是用于送和接电子邮件的 Java API。可以使用 JavaMail API 从多个件人送电子邮件,下面是一个示例: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MultipleSenders { public static void main(String[] args) { // 件人列表 String[] fromList = {"sender1@example.com", "sender2@example.com"}; // 件人 String to = "recipient@example.com"; // SMTP 服务器地址 String host = "smtp.example.com"; // 邮件标题 String subject = "JavaMail Test"; // 邮件内容 String text = "This is a test email from JavaMail."; // 件人用户名和密码 String username = "username"; String password = "password"; // 设置邮件会话属性 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // 获取邮件会话 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(username, password); } }); try { // 创建邮件消息 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromList[0])); for (int i = 1; i < fromList.length; i++) { message.addFrom(InternetAddress.parse(fromList[i])); } message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); message.setText(text); // 送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用 `addFrom()` 方法将多个件人添加到邮件消息中。注意,SMTP 服务器可能会对此进行限制,因此请确保您遵守 SMTP 服务器的规定。另外,也要确保您有权使用添加的所有件人地址。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值