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:
commit
52d830bcbe
@ -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,8 +57,6 @@ public class EmailService {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
Message message = new MimeMessage(session);
|
Message message = new MimeMessage(session);
|
||||||
message.setFrom(new InternetAddress("from@gmail.com"));
|
message.setFrom(new InternetAddress("from@gmail.com"));
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@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";
|
String msg = "This is my first email using JavaMailer";
|
||||||
|
|
||||||
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
||||||
mimeBodyPart.setContent(msg, "text/html");
|
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
|
||||||
|
|
||||||
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
||||||
attachmentBodyPart.attachFile(new File("pom.xml"));
|
|
||||||
|
attachmentBodyPart.attachFile(getFile());
|
||||||
|
|
||||||
Multipart multipart = new MimeMultipart();
|
Multipart multipart = new MimeMultipart();
|
||||||
multipart.addBodyPart(mimeBodyPart);
|
multipart.addBodyPart(mimeBodyPart);
|
||||||
@ -64,14 +78,14 @@ public class EmailService {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
sample attachment content
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user