Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mail - sending works; - but not always visible/available are the attachments  RSS feed

 
Andrew Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm a new member! And not a native english speaker (be lenient!).

Now the problem: I use the java-mail-api to send mails. The sending of mails are working fine and SOMETIMES the attachments are visible and available. When the receiver use a web-browser-mail-client always. When the receiver use outlook or thunderbird never. In the case of thunderbird: when I look the sourceview of the mail, I can see the attachment (in base-64-encoding).

I've looked a lot of other examples and api-descriptions but I can't recognize a hint what I've done wrong.

Here my program:
the relevant method called "multiPartMailMitAnhang"

//begin of code
package de.xxx.xxx.xxx.service;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Properties;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.Locale;

/**
* User: keepitsimple
* last change 14.06.2006 keepitsimple
*/

public class SendJavaMail {

private Session session = null;
private Message msg = null;
private InternetAddress adressFrom = null;
InternetAddress adressTo[] = null;
InternetAddress adressCC[] = null;

private static String appendix = "";
private static String mailtext = "";

private static ResourceBundle bundle = ResourceBundle.getBundle("ressources.ini.common", Locale.GERMAN);

private void prepareMailHeader(String recipient[],
String recipientCC[],
String subject,
String from) throws Exception{
try {
session = null;
msg = null;
adressFrom = null;
adressTo = null;
adressCC = null;

Properties props = new Properties();

props.put("mail.smtp.host", bundle.getString("MAIL.PRODEXTGW"));

session = Session.getDefaultInstance(props);
msg = new MimeMessage(session);
adressFrom = new InternetAddress(from);
msg.setFrom(adressFrom);

adressTo = new InternetAddress[recipient.length];
for (int i = 0; i < recipient.length; i++) {
adressTo[i] = new InternetAddress(recipient[i]);
}
msg.setRecipients(Message.RecipientType.TO, adressTo);

if (recipientCC != null) {
adressCC = new InternetAddress[recipientCC.length];
for (int i = 0; i < recipientCC.length; i++) {
adressCC[i] = new InternetAddress(recipientCC[i]);
}
msg.setRecipients(Message.RecipientType.CC, adressCC);
}
InternetAddress replyAdress[] = new InternetAddress[1];
replyAdress[0] = new InternetAddress(from);
msg.setReplyTo(replyAdress);

msg.setSubject(subject);
} catch (MessagingException e) {
throw new Exception("SendJavaMail.prepareMailHeader ---> " + e.getMessage());
}
}

private void postMail(String recipient[],
String recipientCC[],
String subject,
String message,
String from) throws Exception {

try {
prepareMailHeader(recipient, recipientCC, subject, from);
msg.setContent(message, "text/plain");
Transport.send(msg);
} catch (Exception e) {
throw new Exception("SendJavaMail.prepareMailHeader ---> " + e.getMessage());
}
}

private void multiPartMailMitAnhang(String recipient[],
String recipientCC[],
String subject,
String from
) throws Exception {

try {
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();

text.setText(mailtext);

text.setHeader("MIME-Version", "1.0");
text.setHeader("Content-Type", text.getContentType());

content.addBodyPart(text);

//attachment
// change 14.06.2006 von DataSource fileDatasource ----> FileDataSource fileDatasource
// no affection
FileDataSource fileDataSource = new FileDataSource (appendix);
MimeBodyPart anhang = new MimeBodyPart();
anhang.setDataHandler(new DataHandler(fileDataSource));
anhang.setFileName("edho.pdf");
content.addBodyPart(anhang);

prepareMailHeader(recipient, recipientCC, subject, from);

msg.setContent(content);
msg.setHeader("MIME-Version", "1.0");
msg.setHeader("Content-Type", content.getContentType());
msg.setHeader("X-Mailer", "Java-Mailer V 1.60217733");
msg.setSentDate(new Date());

Transport.send(msg);

} catch (Exception e) {
throw new Exception("SendJavaMail.multiPartMailMitAnhang ---> " + e.getMessage());
}
}

public void sendPostMail(String empfaenger, String absender, String betreff, String mtext) throws Exception{

try {
mailtext = mtext;
String recipient[] = {empfaenger};
String recipientCC[] = null;
postMail(recipient, recipientCC,
betreff,
mailtext,
absender);
} catch (Exception e) {
throw new Exception("SendJavaMail.sendPostMail ---> " + e.getMessage());
}
}

public void sendMultiPartMail(String empfaenger, String absender, String betreff, String mtext) throws Exception{
mailtext = mtext;
String recipient[] = {empfaenger};
String recipientCC[] = {""};
multiPartMail(recipient, recipientCC,
betreff,
absender);
}

public void sendMultiPartMailMitAnhang(String empfaenger, String absender, String betreff, String mtext, String anh) throws Exception{
try {
appendix = anh;
mailtext = mtext;
String recipient[] = {empfaenger};
String recipientCC[] = null;

multiPartMailMitAnhang(recipient, recipientCC,
betreff,
absender);
} catch (Exception e) {
throw new Exception("SendJavaMail.sendMultiPartMailMitAnhang ---> " + e.getMessage());
}

}

public static void main(String[] args) throws Exception{
try {
new SendJavaMail().sendMultiPartMailMitAnhang(args[0], args[1], args[2], args[3], args[4]);

} catch (Exception e) {
e.printStackTrace();
throw e;
}
}


}
//end of code


I am asking for your tipps, experiences!
Thank you!
Kind regards
Andrew
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!