[JAVA-8383] Add charset to email mime type to handle special chars

This commit is contained in:
Haroon Khan 2021-11-17 21:06:29 +00:00
parent f2fd110be1
commit 17838fe083
3 changed files with 113 additions and 38 deletions

View File

@ -1,40 +1,55 @@
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.net.URI;
import java.util.Properties;
public class EmailService {
private String host = "";
private int port = 0;
private String username = "";
private String password = "";
private String username;
private String password;
private final Properties prop;
public EmailService(String host, int port, String username, String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
sendMail();
}
private void sendMail() {
Properties prop = new Properties();
prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.port", port);
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() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
@ -42,8 +57,6 @@ 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"));
@ -52,10 +65,11 @@ public class EmailService {
String msg = "This is my first email using JavaMailer";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html");
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.attachFile(new File("pom.xml"));
attachmentBodyPart.attachFile(getFile());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
@ -64,14 +78,14 @@ public class EmailService {
message.setContent(multipart);
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String ... args) {
new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed");
private File getFile() throws Exception {
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();
}
}