File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes Java mail is being sent as an attachment using ant but as an inline when prog run from eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Java mail is being sent as an attachment using ant but as an inline when prog run from eclipse" Watch "Java mail is being sent as an attachment using ant but as an inline when prog run from eclipse" New topic
Author

Java mail is being sent as an attachment using ant but as an inline when prog run from eclipse

Gupta Ishan
Greenhorn

Joined: Feb 14, 2013
Posts: 3
Hi,
I am facing a strange problem when I am running my java mail code from eclipse it is being sent as an inline message but when same java code is being executed from an ant script than it is being sent as an attachment I have shared the code for sending java mail below, Please help me with this.



package experimental_report;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;

import javax.mail.Message;


import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;








import com.gui_auto.utilities.CommonFunctions;

public class SendMail
{
public static void main(String[] args)
{
Properties mailProps=new Properties();
final String username="user";
final String password="password";
mailProps.put("mail.smtp.host", "smtp.gmail.com");
mailProps.put("mail.smtp.socketFactory.port", "465");
mailProps.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.smtp.port", "465");
Session session=Session.getInstance(mailProps, new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
// TODO Auto-generated method stub
return new PasswordAuthentication(username,password);
}
});

try
{
Message message=new MimeMessage(session);

message.setFrom(new InternetAddress("xyz@gmail.com"));
message.setSubject("Fonts.com sanity automation report for Win7 Firefox 26");
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("abc@mailinator.com"));
DataSource source=new FileDataSource(new File("//pathOfFile/finalReport.html"));
message.setDataHandler(new DataHandler(source));
message.setFileName("finalReport.html");
message.setDisposition(Message.INLINE);
message.saveChanges();
Transport.send(message);

System.out.println("mail sent");

}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
session=null;
mailProps.clear();

}

}
}


ant script

<target name="email">
<java classpathref="WebAutomation.classpath" classname="experimental_report.SendMail">
</java>
</target>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

For a start I would check the classpaths you are using in those two situations and find out whether you are using the same version of JavaMail in both.
Gupta Ishan
Greenhorn

Joined: Feb 14, 2013
Posts: 3
Hi Paul,

I checked the version of java mail in both cases and they are same, Can there be any other thing which is causing this issue?

Gupta Ishan
Greenhorn

Joined: Feb 14, 2013
Posts: 3
Can anyone please help me with this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Look at the headers of the two mail messages which are different. You should find evidence that they were sent by JavaMail and you might find more interesting evidence. At any rate it's probable that the difference is caused by some configuration difference between the two systems. For code it's easy to say "Post your code" but for configuration we can't say "Post your configuration" because that could be all sorts of things. You could also modify your code so that it inserts the JavaMail version number into the message.
 
jQuery in Action, 2nd edition
 
subject: Java mail is being sent as an attachment using ant but as an inline when prog run from eclipse