I try to send a mail with following code snippet:
message = new MimeMessage(mailSession);
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO, to);
if(cc != null && cc.length > 0){
message.setRecipients(Message.RecipientType.CC, cc);
}
if(bcc != null && bcc.length > 0){
message.setRecipients(Message.RecipientType.BCC, bcc);
}
if(replyTo != null && replyTo.length > 0){
message.setReplyTo(replyTo);
}
message.setSubject(subject, "utf-8");
message.setSentDate(new java.util.Date());
if (headers != null && !headers.isEmpty()) {
for (String headName : headers.keySet()) {
message.addHeader(headName, headers.get(headName));
}
}
if (Utils.isEmpty(bodyText)) {
bodyText = HTMLHelper.html2text(body);
}
message.setContent(this.buildMessageBody(body, bodyText));
transporter = mailSession.getTransport();
transporter.connect();
transporter.sendMessage(message, message.getAllRecipients());
Bellow following additional methods:
private Multipart buildMessageBody(String body, String bodyText) throws MessagingException {
if(attachments == null || attachments.isEmpty()){
return getAlternativeBodyMimeMultipart(body, bodyText);
}
MimeMultipart multipartRoot = new MimeMultipart("mixed");
BodyPart contentBodyPart = buildContentBodyPart(body, bodyText);
multipartRoot.addBodyPart(contentBodyPart);
List<BodyPart> attachmentParts = buildAttachmentParts();
for(BodyPart singleAttachmentPart : attachmentParts){
multipartRoot.addBodyPart(singleAttachmentPart);
}
return multipartRoot;
}
private List<BodyPart> buildAttachmentParts() {
List<BodyPart> attachmentsParts = new ArrayList<BodyPart>();
for (int i = 0; i < attachments.size(); i++) {
BinaryAttachment attach = attachments.get(i);
MimeBodyPart mbp = new MimeBodyPart();
System.setProperty("mail.mime.encodefilename", "true");
try {
mbp.setDataHandler(new DataHandler(attach));
mbp.setFileName(MimeUtility.encodeText(attach.getName()));
attachmentsParts.add(mbp);
} catch (Exception e) {
logger.error("buildBodyWithAttachment",e);
}
}
return attachmentsParts;
}
private BodyPart buildContentBodyPart(String body, String bodyText) throws MessagingException {
MimeMultipart alternativePart = getAlternativeBodyMimeMultipart(body, bodyText);
BodyPart content = new MimeBodyPart();
content.setContent(alternativePart);
return content;
}
For exemple my sender in "from" variable when I call messages.setFrom(from) have following value:
"M. Test ADMINISTRATEURÈÁÍ admin#demo.onmicrosoft.com"
But when I receive my mail in my mailbox, the send have the following name ...
M. Test ADMINISTRATEURÃÃÃ
From: "M. Test ADMINISTRATEUR???" admin#demo.onmicrosoft.com
I think the problem come from the encoding of "from" which create by:
from = new InternetAddress(sender) and sender is "M. Test ADMINISTRATEURÈÁÍ admin#demo.onmicrosoft.com".
How can I solve this?
You will have to specify the charset - UTF-8 in the InternetAddress constructor.
from = new InternetAddress(email, sender, "UTF-8")
The constructor from JavaMail for the above code is as below.
/**
* Construct an InternetAddress given the address and personal name.
* The address is assumed to be a syntactically valid RFC822 address.
*
* #param address the address in RFC822 format
* #param personal the personal name
* #param charset the MIME charset for the name
* #exception UnsupportedEncodingException if the personal name
* can't be encoded in the given charset
*/
public InternetAddress(String address, String personal, String charset)
throws UnsupportedEncodingException {
this.address = address;
setPersonal(personal, charset);
}
Related
{"personalizations":[{"to":[{"email":"<EMAIL>"}],"from":{"name":"ABCD","email":"<EMAIL>"},"subject":"Verify OTP","custom_args":{"OTP":"dsgsdf"}}],"template_id":"<TEMPLATE_ID>"}
{{"errors":[{"message":"The from object must be provided for every email send. It is an object that requires the email parameter, but may also contain a name parameter. e.g. {"email" : "example#example.com"} or {"email" : "example#example.com", "name" : "Example Recipient"}.","field":"from.email","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.from"}]}
public void sendEmail(String otp, String email, String toName) {
Email from = new Email();
from.setEmail(sendgridFrom);
from.setName(fromName);
String subject = "Verify OTP";
Email to = new Email();
to.setName(toName);
to.setEmail(email);
DynamicTemplatePersonalization personalization = new DynamicTemplatePersonalization();
personalization.addTo(to);
Mail mail = new Mail();
mail.setFrom(from);
personalization.setSubject(subject);
personalization.addDynamicTemplateData("OTP", otp);
mail.addPersonalization(personalization);
mail.setTemplateId(sendgridTemplate);
SendGrid sg = new SendGrid(sendgridApiKey);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
int currentIndexCount = 0;
int status = response.getStatusCode();
while (status != 202 && currentIndexCount < maxEmailRetries) {
response = sg.api(request);
status = response.getStatusCode();
currentIndexCount++;
}
log.debug(subject, response.getStatusCode());
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static class DynamicTemplatePersonalization extends Personalization {
#JsonProperty(value = "dynamic_template_data")
private Map<String, Object> dynamic_template_data;
#JsonProperty("dynamic_template_data")
public Map<String, Object> getDynamicTemplateData() {
if (dynamic_template_data == null) {
return Collections.<String, Object>emptyMap();
}
return dynamic_template_data;
}
public void addDynamicTemplateData(String key, String value) {
if (dynamic_template_data == null) {
dynamic_template_data = new HashMap<String, Object>();
dynamic_template_data.put(key, value);
} else {
dynamic_template_data.put(key, value);
}
}
}
This code worked for me.
I am developing an application that needs to send an email via JavaMail with a specific Message ID.
I have extended the Java MimeMessage class to override the updateMessageID method so that I can set the message ID myself. The problem is that when I call the Transport.send(msg) method it is not calling the updateMessageID method. I thought perhaps I needed to call the saveChanges() method prior to calling Transport.send(msg). Even when I explicitly call msg.saveChanges() this does not trigger the updateMessageID method to be called.
What makes this all the more wacky is the fact that when I convert my test application to a JSP and run it, the Transport.send(msg) method DOES call the updateMessageID method.
Both my server and my webserver that I tested on are running jdk1.7.0_71.
Extended MimeMessage Class
package com.my.framework;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MYMimeMessage extends MimeMessage {
Session session;
private static int id = 0;
private static String messageID = null;
public MyMimeMessage(Session session) {
super(session);
this.session=session;
}
protected void updateMessageID() throws MessagingException {
System.out.println("Calling updateMessageID()");
setHeader("Message-ID", "<" + getUniqueMessageIDValue(session) + ">");
}
/* Added to pass message id in */
public static void setMessageID(String cid)
{
messageID = cid;
}
public static String getUniqueMessageIDValue(Session ssn) {
String suffix = null;
InternetAddress addr = InternetAddress.getLocalAddress(ssn);
if (addr != null)
suffix = addr.getAddress();
else {
suffix = "javamailuser#localhost"; // worst-case default
}
if(messageID == null)
{
messageID = "987654321";
}
StringBuffer s = new StringBuffer();
// Unique string is <messageID>.<id>.<currentTime>.FDDMail.<suffix>
s.append(messageID).append('.').append(getUniqueId()).append('.').
append(System.currentTimeMillis()).append('.').
append("FDDMail.").
append(suffix);
System.out.println("RETURNING THE new ID: " + s.toString()");
return s.toString();
}
private static synchronized int getUniqueId() {
return id++;
}
}
I call this MimeMessageClass from a mail wrapper called SimpleEmail. It is mostly a bunch of get/set functions. All of the meat is in the sendEmail method...
public String sendEmail()
{
String msgText1 = this.getBody();
// Create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", this.getSmtpClient());
props.put("mail.from", "");
Session session = Session.getDefaultInstance(props, null);
try
{
// Create a message
MyMimeMessage msg = new MyMimeMessage(session);
if (null != sender && sender.length() > 0)
{
msg.setSender(new InternetAddress(sender));
}
if((this.getReply_to() != null) && (this.getReply_to().length() > 0))
{
Address emailReplyTo[] = new Address[1];
emailReplyTo[0] = new InternetAddress(this.getReply_to());
msg.setReplyTo(emailReplyTo);
}
msg.setFrom(new InternetAddress(this.getFrom()));
if(this.to == null || this.to.size() <= 0)
{
return "Error: No To to send";
}
int toIndex = 0;
InternetAddress [] address = new InternetAddress [this.to.size()];
while(this.HasNextTo())
{
address[toIndex] = new InternetAddress(this.nextTo());
toIndex++;
}
msg.setRecipients(Message.RecipientType.TO, address);
if(this.subject == null)
{
this.subject = "<no subject>";
}
msg.setSubject(this.subject);
if(!useTextHeader)
{
//Create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setDataHandler(new DataHandler(new HTMLDataSource(msgText1)));
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
// Create the second message part
MimeBodyPart mbp2;
FileDataSource fds;
String filename;
if(this.attachments != null) {
Set attachmentPathAndNames = this.attachments.keySet();
Iterator attachmentIterator = attachmentPathAndNames.iterator();
while(attachmentIterator.hasNext()) {
String attachmentPathAndName = (String)attachmentIterator.next();
filename = (String)this.attachments.get(attachmentPathAndName);
if(filename == null) {
String[] dirs = attachmentPathAndName.split("\\/");
filename = dirs[dirs.length - 1];
}
mbp2 = new MimeBodyPart();
fds = new FileDataSource(attachmentPathAndName);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(filename);
//Create the Multipart and its parts to it
mp.addBodyPart(mbp2);
}
}
//add the Multipart to the message
msg.setContent(mp);
}
else
{
msg.setText(msgText1);
}
//set the Date: header
msg.setSentDate(new Date());
//set the MessageID Header
msg.setMessageID(this.messageID);
//send the message
try
{
Transport.send(msg);
}
catch(Exception e)
{
System.out.println("STOP WE THREW AN ERROR!!!!!!!!!!!!!!!");
}
}
catch (MessagingException mex)
{
mex.printStackTrace();
System.out.println("Error: SimpleEmail.SendEmail() = Caught MessagingException: " + mex.toString());
return "Error: SimpleEmail.SendEmail() = Caught MessagingException: " + mex.toString();
}
return this.SUCESS_MESSAGE;
}
So, when I call from a JSP I can see the two print statements from MyMimeMessage class
<%# page import="com.ifx.framework.SimpleEmail" %>
<%
String toAddr = request.getParameter("emailAddr");
String mid = request.getParameter("customID");
String SMTP_CLIENT = "myserver.mydomain.com";
String body = "Hi " + toAddr + "!<br>Today we are testing to see if the setting messageID to " + mid + " works!";
String sendResult = "No Email Sent";
if(toAddr != null)
{
SimpleEmail se = new SimpleEmail();
se.addTo(toAddr);
se.setSubject("Testing Headers");
se.setSmtpClient(SMTP_CLIENT);
se.setFrom("cms_zippylube#gointranet.com");
se.setBody(body);
se.setMessageID(mid);
sendResult = se.sendEmail();
}
%>
<!DOCTYPE html>
<html>
<head>
<title>
Test Page
</title>
<style>
label {
width: 200px;
display: inline-block;
margin-bottom: 5px;
}
</style>
</head>
<body>
<p style="background-color: #ADD8E6; border: solid 2px #000080;">
<%=sendResult%>
</p>
<form action="#" method="post">
<label for=emailAddr>Email Address:</label><input id="emailAddr" name="emailAddr" type="email"/> <br>
<label for=customValue>Custom Message ID:</label><input id="customID" name="customID" type="text"/> <br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
In my logs I see:
Calling updateMessage()
RETURNING THE new ID: 8675309.0.1430500125923.FDDMail.javamailuser#localhost
When I check the resulting email, the Message-ID in the header matches what was set.
Here is where my problem is, when I run a standalone version it still sends out the email but doesn't call the updateMessageID method and does not print out the debugging statements.
import com.ifx.framework.SimpleEmail;
public class headerTest
{
public static void main(String args[])
{
String toAddr = args[0];
String mid = args[1];
String SMTP_CLIENT = "myserver.mydomain.com";
String body = "Hi " + toAddr + "!<br>Today we are testing to see if the header message id is retained";
String sendResult = "No Email Sent";
if(toAddr != null)
{
SimpleEmail se = new SimpleEmail();
se.addTo(toAddr);
se.setSubject("Testing Headers");
se.setSmtpClient(SMTP_CLIENT);
se.setFrom("dummy#test.com");
se.setBody(body);
se.setMessageID(mid);
sendResult = se.sendEmail();
}
System.out.println("Done!");
}
}
The only output that I get when I run this is:
Done!
whereas I am expecting
Calling updateMessage()
RETURNING THE new ID: 8675309.0.1430500125923.FDDMail.javamailuser#localhost
Done!
I've got my entire team (including sysadmin) stumped on this issue. Any and All suggestions would be greatly appreciated!
It sounds like you're testing with two different servers so I'm guessing that they're using different versions of JavaMail. What versions are they using? What does the JavaMail debug output show?
I have a problem when I use javamail4ews-0.0.8 for reading mails from MS exchange server 2010
I connect successful
and I read every mail that I have but when I try to see the attachment I only can't see nothing.
I look the contentType his valor is "text/plain" and when I read the getContent().getClass() is String. if I see the information that contain the text/plain this mentioned that is a MimeMultipart beacause have the leyend MIME-Version: 1.0
I look my mail and contain a PDF and XML
I don't know why the program return me a contentType="text/plain" and getContent().getClass()=String
I need one Multipart or MimeMultipart but I don't know how to convert that.
I add my code ....
Thanks guys.
import javax.mail.MessagingException;
import javax.mail.Store;
public class Test {
public static void main(String[] args){
StoreMail test = new StoreMail(
"user",
"password",
"https://<exchangeServer>/ews/exchange.asmx",
"ewsstore",
"C:\\");
//public StoreMail( String userMail, String passMail, String host, String protocolo, String directorio) {
try {
test.readEmails(1);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
and
public class StoreMail {
public StoreMail( String userMail, String passMail, String host, String protocolo, String directorio) {
....
}
public void readEmails( int cantidad ) throws MessagingException {
Properties props = System.getProperties();
props.setProperty("directorio","C:\\BASURA\\\\" );
props.setProperty("dircliente","C:\\BASURA\\\\" );
Session session = Session.getInstance(props, null);
System.out.println( host+" "+userMail+" "+passMail );
Store store = session.getStore(protocolo);
store.connect(host, userMail, passMail);
Folder currFolder = store.getDefaultFolder();
Message[] messages = currFolder.getMessages();
for (Message message : messages) {
try {
Object body = message.getContent();
System.out.println("BODY:::::"+body.getClass()); // is class java.lang.String
if (body instanceof Multipart) { // or instanceof MimeMultipart
System.out.println( " MimeMultipart" );
procMultipart((Multipart) body)
} else {
System.out.println(" other:");
procPart(message);
}
}catch (Throwable thrbl) {
thrbl.printStackTrace();
}
}
}
public void procPart( Part p ) {
try {
String contentType = p.getContentType();
if ( contentType != null ){
//if (p.isMimeType("multipart/alternative")) {
//System.out.println("is multipart/alternative");
//}else{
if ( contentType.toLowerCase().startsWith( "multipart/" ) ) {
procMultipart(( Multipart ) p)
}else{
if(contentType.toLowerCase().startsWith( "text/plain" )){
//I don't know what to do because is a Mime Multipart... and I need to read it
// I need transform the attached to PDF and XML and Save it
}else{
System.out.println("other contentType"+contentType);
String nomfich = p.getFileName();
if (nomfich != null) {
if( nomfich.toLowerCase().endsWith( ".xml" ) ) {
System.out.println(" XML octet-stream nomfich: " + nomfich);
saveArchXML( p.getInputStream() );
} else if( nomfich.toLowerCase().endsWith( ".pdf" ) ) {
saveArchPDF( p.getInputStream(),nomfich.toLowerCase() );
} else if( nomfich.toLowerCase().endsWith( ".zip" ) ) {
saveArchZIP( p.getInputStream(),nomfich.toLowerCase() );
}else{
}
}
}
}
//}
}
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
public void procMultipart(Multipart mp) throws MessagingException {
for (int i = 0; i < mp.getCount(); i++) {
logger.info(" procMultipart :" + i);
Part p = mp.getBodyPart(i);
procPart(p);
}
}
}
You need to learn a lot more about internet email and MIME messages. Start with these JavaMail FAQ entries:
Where can I learn the basics about Internet email that I'll need to know to program JavaMail effectively?
Where can I find some example programs that show how to use JavaMail?
How do I find the main message body in a message that has attachments?
In particular, see the msgshow.java demo program available from the link above.
With JWebServices for Exchange it looks easy
Find messages with attachments
I work with log4j.properties through WSO2 products. I needed implement a appender to work with SMTPAppender and send emails notifications using gmail smtp server. So, when I config log4j and start ESB WSO2 Server, the manage console print: log4j:ERROR Could not instantiate class [com.notification.GmailSMTPAppender].
My implementation was:
package com.notification;
public class GmailSMTPAppender extends SMTPAppender {
protected Session session;
public GmailSMTPAppender() {
super();
}
/**
* Create mail session.
*
* #return mail session, may not be null.
*/
protected Session createSession() {
Properties props = new Properties();
props.put("mail.smtps.host", getSMTPHost());
props.put("mail.smtps.auth", "true");
Authenticator auth = null;
if (getSMTPPassword() != null && getSMTPUsername() != null) {
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(getSMTPUsername(),
getSMTPPassword());
}
};
}
session = Session.getInstance(props, auth);
if (getSMTPProtocol() != null) {
session.setProtocolForAddress("rfc822", getSMTPProtocol());
}
if (getSMTPDebug()) {
session.setDebug(getSMTPDebug());
}
return session;
}
/**
* Send the contents of the cyclic buffer as an e-mail message.
*/
protected void sendBuffer() {
try {
MimeBodyPart part = new MimeBodyPart();
StringBuffer sbuf = new StringBuffer();
String t = layout.getHeader();
if (t != null)
sbuf.append(t);
int len = cb.length();
for (int i = 0; i < len; i++) {
LoggingEvent event = cb.get();
sbuf.append(layout.format(event));
if (layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
if (s != null) {
for (int j = 0; j < s.length; j++) {
sbuf.append(s[j]);
sbuf.append(Layout.LINE_SEP);
}
}
}
}
t = layout.getFooter();
if (t != null)
sbuf.append(t);
part.setContent(sbuf.toString(), layout.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
msg.setContent(mp);
msg.setSentDate(new Date());
send(msg);
} catch (Exception e) {
LogLog.error("Error occured while sending e-mail notification.", e);
}
}
/**
* Pulled email send stuff i.e. Transport.send()/Transport.sendMessage(). So
* that on required this logic can be enhanced.
*
* #param msg
* Email Message
* #throws MessagingException
*/
protected void send(Message msg) throws MessagingException {
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
try {
t.connect(getSMTPHost(), getSMTPUsername(), getSMTPPassword());
t.sendMessage(msg, msg.getAllRecipients());
} finally {
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
}
How I can instantiate of this appender on my log4j.properties configuration ??
Do you need to programatically add the Appender?
Check the following post
I want to modify or remove the "Message-ID" header when replying to an email with Javamail. After some research I found out I need to create a custom class that extends MimeMessage. Here is the class that I have created.
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
class MyMessage extends MimeMessage
{
public MyMessage(Session session)
{
super(session);
}
#Override
protected void updateMessageID() throws MessagingException {
removeHeader("Message-Id");
}
}
The code below is related to fetching the messages
public List<EmailSenderInfo> checkEmail() throws Exception
{
String host = "HOST";
String username = "MYUSERNAME";
String password = "MYPASS";
List<EmailSenderInfo> emailSenderList = new ArrayList<EmailSenderInfo>();
Properties properties = System.getProperties();
properties.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("imaps");
store.connect(host, username, password);
/*
* Folder[] f = store.getDefaultFolder().list(); for (Folder fd : f)
* System.out.println(">> " + fd.getName());
*/
Folder folder = store.getFolder("INBOX");
if (!folder.exists())
{
System.out.println("No INBOX...");
System.exit(0);
}
folder.open(Folder.READ_WRITE);
Message[] msg = folder.getMessages();
if (msg.length < 1)
{
System.out.println("No new messages!");
throw new Exception("No new messages!");
}
for (int i = 0; i < msg.length; i++)
{
if (!msg[i].isSet(Flag.SEEN))
{
EmailSenderInfo emailSenderInfo = new EmailSenderInfo();
emailSenderInfo.message = msg[i];
System.out.println("------------ Message " + (i + 1) + " ------------");
// String from = InternetAddress.toString(msg[i].getFrom());
Address[] fromArray = msg[i].getFrom();
InternetAddress fromInternetAddress = (InternetAddress) fromArray[0];
String from = fromInternetAddress.getAddress();
String fromName = fromInternetAddress.getPersonal();
if (fromName != null)
{
emailSenderInfo.fromName = fromName;
}
if (from != null)
{
// System.out.println("From: " + from);
emailSenderInfo.from = from;
}
// String replyTo = InternetAddress.toString(msg[i].getReplyTo());
Address[] replyToArray = msg[i].getFrom();
InternetAddress ReplyToInternetAddress = (InternetAddress) replyToArray[0];
String replyTo = ReplyToInternetAddress.getAddress();
String replyToName = ReplyToInternetAddress.getPersonal();
if (replyTo != null)
{
// System.out.println("Reply-to: " + replyTo);
emailSenderInfo.replyTo = replyTo;
}
if (replyToName != null)
{
// System.out.println("Reply-to: " + replyTo);
emailSenderInfo.replyToName = replyToName;
}
String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
if (to != null)
{
// System.out.println("To: " + to);
emailSenderInfo.to = to;
}
String subject = msg[i].getSubject();
if (subject != null)
{
// System.out.println("Subject: " + subject);
emailSenderInfo.subject = subject;
}
Date sentDate = msg[i].getSentDate();
if (sentDate != null)
{
System.out.println("Sent: " + sentDate);
emailSenderInfo.sentDate = sentDate;
}
String bodyHtml = "";
// get content
Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++)
{
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
{
// dealing with attachments only
System.out.println("Mail has some attachment : ");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
System.out.println("ddddd: " + bodyPart.getContent().toString());
}
else
{
String classType = bodyPart.getContent().getClass().toString();
if (classType.contains("java.lang.String"))
{
bodyHtml = bodyPart.getContent().toString();
}
else if (classType.contains("javax.mail.internet.MimeMultipart"))
{
MimeMultipart bodyContent = (MimeMultipart) bodyPart.getContent();
for (int b = 0; b < bodyContent.getCount(); b++)
{
IMAPBodyPart imapBody = (IMAPBodyPart) bodyContent.getBodyPart(b);
System.out.println("1: " + imapBody.getContent());
bodyHtml = imapBody.getContent().toString();
// System.out.println("2: " + bodyContent.getBodyPart(b));
// System.out.println("3: " + bodyPart.getContent().toString());
}
}
}
emailSenderInfo.bodyHtml = bodyHtml;
}
MyMessage reply = (MyMessage) msg[i].reply(false);
emailSenderInfo.reply = reply;
// reply.setFrom(msg[i].getFrom()[0]);
MimeMessage orig = (MimeMessage) msg[i];
StringBuffer buffer = new StringBuffer("Thanks\n\n");
if (orig.isMimeType("text/plain"))
{
String content = (String) orig.getContent();
StringReader contentReader = new StringReader(content);
BufferedReader br = new BufferedReader(contentReader);
String contentLine;
while ((contentLine = br.readLine()) != null)
{
buffer.append("> ");
buffer.append(contentLine);
buffer.append("\r\n");
}
}
// Set the reply content
// reply.setText(buffer.toString());
// emailSenderInfo.reply = reply;
emailSenderList.add(emailSenderInfo);
// System.out.println();
}// end if unread
}
folder.close(true);
store.close();
return emailSenderList;
}
I have two methods in my program. One of them checks mail and another one which replies to emails.
Message reply = msg[i].reply(false);
"reply" gets passed to this method along with other parameters.
public void sendReply(String from, String replyTo, Message reply, String messageReply, Boolean attachment) throws Exception
{
String host = "MYHOST";
String username = "MYUSERNAME";
String pass = "MYPASSWORD";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage mimeReply = (MimeMessage) reply;
mimeReply.setFrom((Address) InternetAddress.parse(from)[0]);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageReply, "text/html");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
if (attachment)
{
messageBodyPart = new MimeBodyPart();
String filename = "test.jpg";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}
mimeReply.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, pass);
transport.sendMessage(mimeReply, InternetAddress.parse(replyTo));
transport.close();
System.out.println("Message Sent!");
}
I have to use the MyMessage class in order to remove the "Message-ID" header. I have tried the following
MyMessage mimeReply = (MyMessage) reply;
But I get the following error in runtime
java.lang.ClassCastException: javax.mail.internet.MimeMessage cannot be cast to javamailer.MyMessage
How can use "MyMessage" class so I can remove the "Message-ID" header with the reply message?
You can try the following:
When replying to an email:
When you create a message create, right now it should be like:
MimeMessage msg = new MimeMessage(session);
change it to,
MyMessage msg = new MyMessage(session);
When checking email you don't need to remove the header as that message is already in the mailbox, what I think you need to do is when replying to a mail at that instance for the name mail instantiate like :
MyMessage msg = new MyMessage(session);
msg.updateMessageID();
Since you are using reference to existing message:
You can do something like:
Create a new constructor:
public MyMessage (MimeMessage message) {
super(message);
}
When replying:
MyMessage mimeReply = new MyMessage(reply);
mimeReply.updateMessageID();
Then send the mimeReply NOT reply.