如何用Java编写代码实现发送带附件的QQ邮箱邮件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计970个文字,预计阅读时间需要4分钟。
本文分享了一个Java实现发送QQ邮箱带附件邮件的具体代码示例。以下为代码内容:
javaimport java.util.Properties;import javax.mail.*;import javax.mail.internet.*;
public class EmailSender { public static void main(String[] args) { // 设置邮件服务器和端口 String smtpServer=smtp.qq.com; int smtpPort=465;
// 设置发件人邮箱和密码 String senderEmail=your_qq_email@qq.com; String senderPassword=your_qq_email_password;
// 设置收件人邮箱 String receiverEmail=receiver@example.com;
// 设置邮件主题和内容 String subject=邮件主题; String content=邮件内容;
// 设置附件路径 String filePath=path_to_attachment;
// 创建邮件会话 Properties props=new Properties(); props.put(mail.smtp.auth, true); props.put(mail.smtp.starttls.enable, true); props.put(mail.smtp.host, smtpServer); props.put(mail.smtp.port, String.valueOf(smtpPort));
// 创建认证信息 Authenticator auth=new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmail, senderPassword); } };
// 获取Session对象 Session session=Session.getInstance(props, auth);
try { // 创建MimeMessage对象 Message message=new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverEmail)); message.setSubject(subject); message.setText(content);
// 添加附件 MimeMultipart multipart=new MimeMultipart(); BodyPart messageBodyPart=new MimeBodyPart(); messageBodyPart.setText(content); multipart.addBodyPart(messageBodyPart);
messageBodyPart=new MimeBodyPart(); messageBodyPart.attachFile(filePath); multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送邮件 Transport.send(message); System.out.println(邮件发送成功!); } catch (MessagingException | IOException e) { e.printStackTrace(); } }}
使用说明:
1. 将`your_qq_email@qq.com`替换为你的QQ邮箱账号。
2.将`your_qq_email_password`替换为你的QQ邮箱密码(建议使用授权码)。
3.将`receiver@example.com`替换为收件人邮箱地址。
4.将`path_to_attachment`替换为附件的路径。
依赖:
xml
javax.mail mail本文实例为大家分享了Java实现邮件发送QQ邮箱带附件的具体代码,供大家参考,具体内容如下
添加依赖
<!-- mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
关键代码
import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.PasswordAuthentication; 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; /** * 邮件发送工具类 <br/> * Author:杨杰超<br/> * Date:2020年1月9日 下午12:02:51 <br/> * Copyright (c) 2020, yangjiechao@dingtalk.com All Rights Reserved.<br/> * */ public class SendMail { /** * 想QQ邮箱发送邮件 * * @param formMail * 发送人邮箱地址 * @param descMail * 接收人邮箱地址 * @param subject * 邮箱主题 * @param content * 邮箱内容 * @param files * 附件列表 * @param contentType * 内容格式 * @param password * SMTP密码 * @throws MessagingException * @throws UnsupportedEncodingException */ public static void sendQQMail(String formMail, String descMail, String subject, String content, File[] files, String contentType, String password) throws MessagingException, UnsupportedEncodingException { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.qq.com"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.debug", "true"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(formMail, password); } }); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(formMail)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(descMail)); message.setSubject(subject); // 是否存在附件 if (null != files && files.length > 0) { MimeMultipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content, contentType); multipart.addBodyPart(contentPart); for (File file : files) { MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource(file)); attachment.setDataHandler(dh2); attachment.setFileName(MimeUtility.encodeText(dh2.getName())); multipart.addBodyPart(attachment); } multipart.setSubType("mixed"); message.setContent(multipart); message.saveChanges(); } // 普通 else { message.setContent(content, contentType); } Transport transport = session.getTransport(); transport.connect(formMail, password); Transport.send(message); } catch (UnsupportedEncodingException e) { throw e; } catch (NoSuchProviderException e) { throw e; } catch (MessagingException e) { throw e; } } public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { // 由哪个邮箱发送 String formMail = "********@qq.com"; // QQ邮箱>设置>账户 开启POP3/SMTP服务 查看smtp密码 String smtpPassword = "****************"; // 发送人邮箱地址 String descMail = "470947852@qq.com"; String contentType = "text/html;charset=UTF-8"; String subject = "测试邮件发送,含附件"; String content = "test send mail, 这里是中文"; File[] files = new File[2]; files[0] = new File("C:/test_1.xls"); files[1] = new File("C:/test_2.xls"); SendMail.sendQQMail(formMail, descMail, subject, content, files, contentType, smtpPassword); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计970个文字,预计阅读时间需要4分钟。
本文分享了一个Java实现发送QQ邮箱带附件邮件的具体代码示例。以下为代码内容:
javaimport java.util.Properties;import javax.mail.*;import javax.mail.internet.*;
public class EmailSender { public static void main(String[] args) { // 设置邮件服务器和端口 String smtpServer=smtp.qq.com; int smtpPort=465;
// 设置发件人邮箱和密码 String senderEmail=your_qq_email@qq.com; String senderPassword=your_qq_email_password;
// 设置收件人邮箱 String receiverEmail=receiver@example.com;
// 设置邮件主题和内容 String subject=邮件主题; String content=邮件内容;
// 设置附件路径 String filePath=path_to_attachment;
// 创建邮件会话 Properties props=new Properties(); props.put(mail.smtp.auth, true); props.put(mail.smtp.starttls.enable, true); props.put(mail.smtp.host, smtpServer); props.put(mail.smtp.port, String.valueOf(smtpPort));
// 创建认证信息 Authenticator auth=new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmail, senderPassword); } };
// 获取Session对象 Session session=Session.getInstance(props, auth);
try { // 创建MimeMessage对象 Message message=new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverEmail)); message.setSubject(subject); message.setText(content);
// 添加附件 MimeMultipart multipart=new MimeMultipart(); BodyPart messageBodyPart=new MimeBodyPart(); messageBodyPart.setText(content); multipart.addBodyPart(messageBodyPart);
messageBodyPart=new MimeBodyPart(); messageBodyPart.attachFile(filePath); multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送邮件 Transport.send(message); System.out.println(邮件发送成功!); } catch (MessagingException | IOException e) { e.printStackTrace(); } }}
使用说明:
1. 将`your_qq_email@qq.com`替换为你的QQ邮箱账号。
2.将`your_qq_email_password`替换为你的QQ邮箱密码(建议使用授权码)。
3.将`receiver@example.com`替换为收件人邮箱地址。
4.将`path_to_attachment`替换为附件的路径。
依赖:
xml
javax.mail mail本文实例为大家分享了Java实现邮件发送QQ邮箱带附件的具体代码,供大家参考,具体内容如下
添加依赖
<!-- mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
关键代码
import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.PasswordAuthentication; 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; /** * 邮件发送工具类 <br/> * Author:杨杰超<br/> * Date:2020年1月9日 下午12:02:51 <br/> * Copyright (c) 2020, yangjiechao@dingtalk.com All Rights Reserved.<br/> * */ public class SendMail { /** * 想QQ邮箱发送邮件 * * @param formMail * 发送人邮箱地址 * @param descMail * 接收人邮箱地址 * @param subject * 邮箱主题 * @param content * 邮箱内容 * @param files * 附件列表 * @param contentType * 内容格式 * @param password * SMTP密码 * @throws MessagingException * @throws UnsupportedEncodingException */ public static void sendQQMail(String formMail, String descMail, String subject, String content, File[] files, String contentType, String password) throws MessagingException, UnsupportedEncodingException { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.qq.com"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.debug", "true"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(formMail, password); } }); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(formMail)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(descMail)); message.setSubject(subject); // 是否存在附件 if (null != files && files.length > 0) { MimeMultipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content, contentType); multipart.addBodyPart(contentPart); for (File file : files) { MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource(file)); attachment.setDataHandler(dh2); attachment.setFileName(MimeUtility.encodeText(dh2.getName())); multipart.addBodyPart(attachment); } multipart.setSubType("mixed"); message.setContent(multipart); message.saveChanges(); } // 普通 else { message.setContent(content, contentType); } Transport transport = session.getTransport(); transport.connect(formMail, password); Transport.send(message); } catch (UnsupportedEncodingException e) { throw e; } catch (NoSuchProviderException e) { throw e; } catch (MessagingException e) { throw e; } } public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { // 由哪个邮箱发送 String formMail = "********@qq.com"; // QQ邮箱>设置>账户 开启POP3/SMTP服务 查看smtp密码 String smtpPassword = "****************"; // 发送人邮箱地址 String descMail = "470947852@qq.com"; String contentType = "text/html;charset=UTF-8"; String subject = "测试邮件发送,含附件"; String content = "test send mail, 这里是中文"; File[] files = new File[2]; files[0] = new File("C:/test_1.xls"); files[1] = new File("C:/test_2.xls"); SendMail.sendQQMail(formMail, descMail, subject, content, files, contentType, smtpPassword); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

