Merge pull request #11467 from hkhan/JAVA-8383-fix-email-code

[JAVA-8383] Add charset to email mime type to handle special chars
This commit is contained in:
kwoyke 2021-11-18 09:59:15 +01:00 committed by GitHub
commit 52d830bcbe
3 changed files with 113 additions and 38 deletions

View File

@ -1,40 +1,55 @@
package com.baeldung.mail; package com.baeldung.mail;
import javax.mail.*; import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import java.io.File; import java.io.File;
import java.net.URI;
import java.util.Properties; import java.util.Properties;
public class EmailService { public class EmailService {
private String host = ""; private String username;
private int port = 0; private String password;
private String username = "";
private String password = "";
private final Properties prop;
public EmailService(String host, int port, String username, String password) { public EmailService(String host, int port, String username, String password) {
prop = new Properties();
this.host = host;
this.port = port;
this.username = username;
this.password = password;
sendMail();
}
private void sendMail() {
Properties prop = new Properties();
prop.put("mail.smtp.auth", true); prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", host); prop.put("mail.smtp.host", host);
prop.put("mail.smtp.port", port); prop.put("mail.smtp.port", port);
prop.put("mail.smtp.ssl.trust", host); prop.put("mail.smtp.ssl.trust", host);
this.username = username;
this.password = password;
}
public EmailService(String host, int port) {
prop = new Properties();
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.port", port);
}
public static void main(String... args) {
try {
new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed")
.sendMail();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMail() throws Exception {
Session session = Session.getInstance(prop, new Authenticator() { Session session = Session.getInstance(prop, new Authenticator() {
@Override @Override
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
@ -42,36 +57,35 @@ public class EmailService {
} }
}); });
try { Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@gmail.com"));
message.setSubject("Mail Subject");
Message message = new MimeMessage(session); String msg = "This is my first email using JavaMailer";
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@gmail.com"));
message.setSubject("Mail Subject");
String msg = "This is my first email using JavaMailer"; MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
MimeBodyPart mimeBodyPart = new MimeBodyPart(); MimeBodyPart attachmentBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html");
MimeBodyPart attachmentBodyPart = new MimeBodyPart(); attachmentBodyPart.attachFile(getFile());
attachmentBodyPart.attachFile(new File("pom.xml"));
Multipart multipart = new MimeMultipart(); Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart);
multipart.addBodyPart(attachmentBodyPart); multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart); message.setContent(multipart);
Transport.send(message); Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
} }
public static void main(String ... args) { private File getFile() throws Exception {
new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed"); URI uri = this.getClass()
.getClassLoader()
.getResource("attachment.txt")
.toURI();
return new File(uri);
} }
} }

View File

@ -0,0 +1 @@
sample attachment content

View File

@ -0,0 +1,60 @@
package com.baeldung.mail;
import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class EmailServiceLiveTest {
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
private EmailService emailService;
@Before
public void setup() {
emailService = new EmailService("localhost", greenMail.getSmtp().getPort());
}
@Test
public void givenEmailMessageWithAttachment_whenEmailIsSent_MessageIsReceived() throws Exception {
emailService.sendMail();
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
assertEquals(1, receivedMessages.length);
MimeMessage receivedMessage = receivedMessages[0];
assertEquals("Mail Subject", subjectFromMessage(receivedMessage));
assertEquals("This is my first email using JavaMailer", emailTextFrom(receivedMessage));
assertEquals("sample attachment content", attachmentContentsFrom(receivedMessage));
}
private static String subjectFromMessage(MimeMessage receivedMessage) throws MessagingException {
return receivedMessage.getSubject();
}
private static String emailTextFrom(MimeMessage receivedMessage) throws IOException, MessagingException {
return ((MimeMultipart) receivedMessage.getContent())
.getBodyPart(0)
.getContent()
.toString();
}
private static String attachmentContentsFrom(MimeMessage receivedMessage) throws Exception {
return ((MimeMultipart) receivedMessage.getContent())
.getBodyPart(1)
.getContent()
.toString();
}
}