This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
In many projects we come across the ability to send email
- In many projects, we will encounter the function of sending email, sending email is actually very practical, just today to do the project needs to implement, now let’s simply arrange the implementation of sending email.
Establish a session between the mail and the server
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.debug", "true");
Session session = Session.getInstance(props);
Copy the code
- I’m using Sina’s mail server here,
props.setProperty("mail.transport.protocol", "smtp");
You need to authenticate your login with SMTPprops.setProperty("mail.smtp.host", "smtp.sina.com");
Indicate to use is sina server, self test currently QQ server is charged, sina this server is free, but can’t send too many emails continuously, otherwise will send failureprops.setProperty("mail.smtp.auth", "true");
We can send the email after the user name and password have been verified successfullyprops.setProperty("mail.debug", "true");
Open debug mode, is for the convenience of our development, we have this in the development of the console we can see the email sending details and sending status, return 250 status code, it is sent successfully.Session session = Session.getInstance(props);
With this session in hand, we can send an email
Create a mail
Message msg = new MimeMessage(session);
Copy the code
MSG here is our mail, but now the mail is still empty, we need to add text, HTML, attachment, subject… Information.
-
The first step:
Set the sender of the email to MSG. SetFrom (new InternetAddress(put your email here));
-
The second part:
Set the subject of the email (the subject is the subject of the email, I won’t explain this, if you upload the attachment this will also be the compressed name of the attachment you download) MSG. SetSubject (” training class completion information “);
-
Third part:
Set up the attachment of the email, the attachment here we need to get the address of the attachment on the hard drive, and here I've written a method, ``` public static MimeBodyPart createAttachment(String fileName) throws Exception { MimeBodyPart attachmentPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileName); / / get data source attachmentPart. SetDataHandler (new DataHandler (FDS)); / / get the attachment itself and to the attachmentPart attachmentPart. SetFileName (FDS) getName ()); return attachmentPart; // Get the file name and add the BodyPart} to the message (MSG) MimeBodyPart attachment01 = createAttachment(filePath); MimeMultipart allPart = new MimeMultipart("mixed");Copy the code
allPart.addBodyPart(attachment01); msg.setContent(allPart); ` ` `
-
Specify sending address
``` msg.setRecipient(RecipientType.TO, new InternetAddress(address)); ` ` `Copy the code
Send E-mail
-
Here we have the email. The subject, content and attachment of the email have been set. The next thing we do is to send the email
``` Transport trans = session.getTransport(); Trans. connect(account to send email, password to send email); trans.sendMessage(msg, msg.getAllRecipients()); ` ` `Copy the code
http://download.csdn.net/detail/u013132051/9642347
Copy the code