Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to send email and carry attachments with Java

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces Java how to send mail and carry attachments, the article is very detailed, has a certain reference value, interested friends must read!

1. Mail Server and Transmission Protocol

To achieve mail functionality on the network, there must be a dedicated mail server. It is mainly responsible for receiving emails from users and delivering emails to email recipients.

SMTP server address: generally smtp.xxx.com, 163 mailbox is smtp.163.com, qq mailbox is smtp.qq.com.

SMTP protocol

The server that handles user smtp requests (mail sending requests) is usually called an SMTP server (mail sending server).

POP3 protocol

A server that processes a user's pop3 request (mail reception request) is generally referred to as a POP3 server (mail reception server).

2. Open POP3/SMTP service in sender mailbox

QQ mailbox

After logging in qq mailbox → Settings → Accounts → POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service → POP3/SMTP service click to open (need to bind mobile phone number to send verification SMS) → Get a string of authorization codes (need to send emails later)

163 mailbox

Login 163 Mailbox → Settings → POP3/SMTP/IMAP → POP3/SMTP service Click to open (requires mailbox app authentication) → Get a string of authorization codes

III. Import Dependency javax.mail mail 1.5.0-b01 IV. Writing tool classes import lombok.extern.slf4j.Slf4j;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.*; import java.util.Date;import java.util.Properties; @Slf4jpublic class EmailUtils { public static final String SenderEmail = "xxxxx@qq.com";//SenderEmail public static final String senderCode = "ixxxdcd";//sender mailbox authorization code public static final String emailSMTPHost = "smtp.qq.com";//Server address public static final String receiveMailAccount = "xxxxx@qq.com";//recipient mailbox public static final String ccMailAccount = "xxxxx@163.com";//cc person mailbox public static final String bccmailAccount = "xxxxxx@qq.com";//Send email /* Send email */ public static void sendMail() { try { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp");//protocol used props.setProperty("mail.smtp.host", emailSMTPHost);//SMTP server address of sender's mailbox props.setProperty("mail.smtp.auth", "true");//authentication request required Session = Session.getInstance(props);//Get session object instance session.setDebug(false);//Whether to print detailed log MimeMessage message = createMimeMessage(session);//Gets the message object (encapsulates a method) Transport transport = session.getTransport(); transport.connect(SenderEmail, senderCode);//Connect the sender's email account // 6. Send mail to all recipients, message.getAllRecipients() gets all recipients added when creating the mail object, CC, BCC transport.sendMessage(message, message.getAllRecipients()); // 7. close the connection transport.close(); log.info ("Mail sent successfully"); } catch (Exception e) { log.error("Failed to send mail"); } } public static MimeMessage createMimeMessage(Session session) throws Exception { // 1. Create an email MimeMessage message = new MimeMessage(session); // 2. From: sender message.setFrom(new InternetAddress(SenderEmail, "From, ""UTF-8")); // 3. Set recipient, CC, BCC //MimeMessage.RecipientType.TO: Receipt Type;MimeMessage.RecipientType.CC: CC Type;MimeMessage.RecipientType.BCC: Secret Send Type message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "Recipients, ""UTF-8")); message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(ccMailAccount, "CC Person", "UTF-8")); message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bccmailAccount, "人", "UTF-8")); // 4. Subject: Mail subject message.setSubject("This is the subject of the message", "UTF-8"); // 5. Content: Message body (html tag may be used) message.setContent("This is the message body", "text/html;charset= UTF-8"); ************************* MimeMultipart multipart = new MimeMultipart(); MimeBodyPart file1 = new MimeBodyPart(); DataHandler handler = new DataHandler(new FileDataSource("FilePath")); file1.setDataHandler(handler); //Encode file names to prevent garbled characters String fileName = MimeUtility.encodeWord("filename", "utf-8", "B"); file1.setFileName(fileName); multipart.addBodyPart(file1); message.setContent(multipart);******************************************************************************************* // 6. Set the time to send message.setSentDate(new Date()); // 7. save settings message.saveChanges(); return message; }} The above is "Java how to send mail and carry attachments" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report