My program can send html emails with attachment, I can get the emails from my Yahoo, Gmail and Lotus Notes accounts. The strange thing is : although the email sizes are the same in all three accounts, my Yahoo account isn't showing the attachment, from the email size I can tell the attachment is in fact in the email I got at Yahoo, it's just not showing it, from my Gmail and Lotus Notes accounts I can see there is an attachment and I can click and open them normally. I guess Yahoo might not like the way I specify the attachment, can someone point out why ? Here is my code :
public void Send_Html_Email(String from,String to,String cc,String bcc,String host,String smtpPort,String subject,String Text_Body_File_Path,String Html_Body_File_Path,String Images_In_Html[][],String fileAttachment,boolean ssl,boolean auth,String username,String password) throws Exception
{
String Attachments[];
Properties props=new Properties(); // Create some properties and get the default Session
props.put("mail.smtp.host",host);
props.put("mail.smtp.port",smtpPort);
// props.put("mail.debug","true");
// Out("from = "+from+"\n to ="+to+"\n cc = "+cc+"\n bcc = "+bcc+"\n host = "+host+"\n smtpPort = "+smtpPort+"\n Text_Body_File_Path = "+Text_Body_File_Path+"\n Html_Body_File_Path = "+Html_Body_File_Path+
// "\n Images_In_Html = "+Images_In_Html+"\n fileAttachment = "+fileAttachment+"\n ssl = "+ssl+"\n auth = "+auth+"\n username = "+username+"\n password = "+password);
Session session=null;
Outgoing_Server_Protocol=ssl?"smtps":"smtp";
if (auth && username != null && password != null)
{
props.put("mail."+Outgoing_Server_Protocol+".auth","true");
session=Session.getInstance(props,new SMTPAuthenticator(username,password));
}
else session=Session.getInstance(props,null);
if (Debug) session.setDebug(true);
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] To_Address=InternetAddress.parse(to.replace(";",","),false);
message.setRecipients(Message.RecipientType.TO,To_Address);
InternetAddress[] Cc_Address=InternetAddress.parse(cc.replace(";",","),false);
message.addRecipients(Message.RecipientType.CC,Cc_Address);
InternetAddress[] Bcc_Address=InternetAddress.parse(bcc.replace(";",","),false);
message.addRecipients(Message.RecipientType.BCC,Bcc_Address);
message.setSubject(subject);
// You may need to encode the subject with this special method !
// message.setSubject(javax.mail.internet.MimeUtility.encodeText("Testing javamail with french : ��� "));
Multipart multipart=new MimeMultipart("alternative"); // Create a "related" Multipart message
if (Use_Alternative_Text)
{
// Read text file, load it into a BodyPart, and add it to the message.
BodyPart alt_bp1=getFileBodyPart(Text_Body_File_Path);
multipart.addBodyPart(alt_bp1);
}
Multipart html_mp=new MimeMultipart("related"); // Include an HTML version with images. The HTML file is a Multipart of type "related". Inside it are two BodyParts: the HTML file and an image
BodyPart rel_bph=getFileBodyPart(Html_Body_File_Path); // Get the HTML file
html_mp.addBodyPart(rel_bph);
String cid;
MimeBodyPart rel_bpi;
FileDataSource ifds; // Get the image file
if (Images_In_Html!=null)
for (int i=0;i<Images_In_Html.length;i++)
{
cid=Images_In_Html[0];
rel_bpi=new MimeBodyPart();
ifds=new FileDataSource(Images_In_Html[i][1]); // Get the image file
// Initialize and add the image file to the html body part
rel_bpi.setFileName(ifds.getName());
rel_bpi.setText("Image "+(i+1));
rel_bpi.setDataHandler(new DataHandler(ifds));
rel_bpi.setHeader("Content-ID","<"+cid+">");
rel_bpi.setDisposition("inline");
html_mp.addBodyPart(rel_bpi);
}
// Create the second BodyPart of the multipart/alternative, set its content to the html multipart, and add the second bodypart to the main multipart.
BodyPart alt_bp2=new MimeBodyPart();
alt_bp2.setContent(html_mp);
multipart.addBodyPart(alt_bp2);
//======
if (fileAttachment != null) // Part two is attachment
{
if (fileAttachment.indexOf(",")!=-1) Attachments=fileAttachment.split(",");
else if (fileAttachment.indexOf(";")!=-1) Attachments=fileAttachment.split(";");
else Attachments=new String[]{fileAttachment};
Out("Attachments.length="+Attachments.length);
for (int i=0;i<Attachments.length;i++)
{
if (new File(Attachments[i]).exists())
{
MimeBodyPart messageBodyPart=new MimeBodyPart(); // Create the message part
FileDataSource fds=new FileDataSource(Attachments[i]);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setFileName(Attachments[i]);
multipart.addBodyPart(messageBodyPart);
}
}
}
//======
// Set the content for the message and transmit
message.setContent(multipart);
// The simple way to send a message is : Transport.send(message);
// But we're going to use some SMTP-specific features for demonstration purposes so we need to manage the Transport object explicitly.
SMTPTransport t=(SMTPTransport)session.getTransport(Outgoing_Server_Protocol);
try
{
if (auth && username != null && password != null) t.connect(host,username,password);
else t.connect();
t.sendMessage(message,message.getAllRecipients());
Email_Sent_B=true;
}
catch (Exception e)
{
Outgoing_Email_Host_Available=false;
Email_Sent_B=false;
if (t.getLastServerResponse()==null)
{
Mail_Result=" No connection from outgoing server : "+host+" [ "+Get_Time.Get_Time_Format(43)+" ]";
if (Mail_Log_TextArea!=null) Mail_Log_TextArea.append(Mail_Result+"\n");
Out(Mail_Result);
}
else
{
Mail_Result=" Outgoing Server Response [ "+host+" ] : "+t.getLastServerResponse().trim()+" [ "+Get_Time.Get_Time_Format(43)+" ]";
if (Mail_Log_TextArea!=null) Mail_Log_TextArea.append(Mail_Result+"\n");
Out(Mail_Result);
if (t.getLastServerResponse().indexOf("Daily sending quota exceeded")!=-1) Daily_Sending_Quota_Exceeded_B=true;
else e.printStackTrace();
}
}
finally
{
if (Verbose) System.out.println(" Outgoing Server Response [ "+host+" ] : "+t.getLastServerResponse().trim()+" [ "+Get_Time.Get_Time_Format(43)+" ]");
try { t.close(); }
catch (Exception ex) { }
// catch (Exception ex) { ex.printStackTrace(); }
}
session=null;
}
public BodyPart getFileBodyPart(String filename) throws MessagingException
{
BodyPart bp=new MimeBodyPart();
bp.setDataHandler(new DataHandler(new FileDataSource(filename)));
return bp;
}
I suspect the problem occurs in between the "//======" lines ( around // Part two is attachment ), and Yahoo is more strict in format then the other 2 accounts.
Frank