dcsimg
How to send mail WITHOUT attachment in JAVAMAIL?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   swapnil_ch
Posted On:   Friday, October 22, 2010 08:45 AM

> In the following code for setFileAsAttachment(msg,FileName,message); if file is provided for attached then it is working but I want to check is file is provided or not and if not provided then how to send mail without attachment? public void SendMailAttachment(String from,String to, String FileName, String subject, String message) { boolean debug = false; try { //Get all Properties from properties File Properties props = new Properties(); InputStream fin = null; try { fin = EMailAttachmentMailer.class.getResourceAsStream("EMail.properties"); props.load(fin); fin.close(); } catch(Exception e)    More>>
>
			
In the following code for
setFileAsAttachment(msg,FileName,message);
if file is provided for attached then it is working but I want to check is file is provided or not and if not provided then how to send mail without attachment?


public void SendMailAttachment(String from,String to, String FileName, String subject, String message)
{
boolean debug = false;
try
{
//Get all Properties from properties File
Properties props = new Properties();
InputStream fin = null;
try
{
fin = EMailAttachmentMailer.class.getResourceAsStream("EMail.properties");
props.load(fin);
fin.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress [] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress(to);
msg.setRecipients(Message.RecipientType.TO, addressTo);
Transport bus = session.getTransport("smtp");
bus.connect();
msg.setSubject(subject);
msg.setContent(message, "text/plain");
setFileAsAttachment(msg,FileName,message);
msg.saveChanges();
bus.sendMessage(msg,addressTo);
bus.close();
}
catch(MessagingException mex)
{
mex.printStackTrace();
}
}

   <<Less
About | Sitemap | Contact