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>