java email

51CTO2022-05-14 19:33:26


import com.qm.be.model.EmailMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.Properties;


public class MailOperation {
private static final Logger logger = LoggerFactory.getLogger(MailOperation.class);
/**
* Send E-mail
* @param emailMessage
* @return
* @throws Exception
*/
public static void sendMail(EmailMessage emailMessage) {
logger.info(emailMessage.toString());
if (emailMessage.to != null){

Properties props = System.getProperties();
// props.put("mail.smtp.port", "25");
props.put("mail.smtp.port", emailMessage.getPort());
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", emailMessage.getHost());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.ssl.trust",emailMessage.getHost());



Session session;
if (emailMessage.getUser() != null && emailMessage.getPassword() != null){
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailMessage.getUser(),emailMessage.getPassword());
}
};
session = Session.getInstance(props,auth);
}else {
session = Session.getInstance(props);
}

session.setDebug(true);

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailMessage.from));
if (!"".equals(emailMessage.to.trim())) {
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emailMessage.to.trim()));
}

if (emailMessage.getCc() != null && emailMessage.getCc().size() > 0) {
for (String address: emailMessage.cc) {
if(!"".equals(address.trim())) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(address.trim()));
}
}
}
// The theme
message.setSubject(emailMessage.subject);

// Content
BodyPart contentBodyPart = new MimeBodyPart();
//add email content part.
// Set the text content of the message
contentBodyPart.setContent(emailMessage.getContent(), "text/html;charset=utf-8");

// Entire mail : Text + The attachment
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(contentBodyPart);

if(emailMessage.getFiles()!=null && emailMessage.getFiles().size()>0){
for(File file:emailMessage.getFiles()){
MimeBodyPart mimeBodyPart = new MimeBodyPart();
// Set the file path to send attachments
DataSource source = new FileDataSource(file.getPath());
mimeBodyPart.setDataHandler(new DataHandler(source));

// Processing attachment name Chinese ( Attached file path ) The code problem
mimeBodyPart.setFileName(MimeUtility.encodeText(file.getName()));
multipart.addBodyPart(mimeBodyPart);
}
}

message.setContent(multipart);
message.addHeader("X-Mailer", "JavaMail");
message.setSentDate(new Date());
message.saveChanges();

Transport transport=session.getTransport("smtp");
transport.connect();
Transport.send(message, message.getAllRecipients());
transport.close();
} catch (Exception e){
logger.error(" fail in send ", e);
e.printStackTrace();
}
}
}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.






thank
Similar articles