import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class HtmlJavaMail {
public static void main(String[] args) {
HtmlJavaMail mjm = new HtmlJavaMail();
mjm.sendMail();
}
public HtmlJavaMail() {}
public void sendMail() {
Properties props = new Properties();
props.put("mail.smtp.host", "mail.syerra.com");
props.put("mail.debug","true");
Session session = Session.getDefaultInstance(props, new ForcedAuthenticator());
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("sujeev@theoturnermedia.com",
"sujeeve"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"kashun@theoturnermedia.com"));
message.setSubject("subject");
MimeMultipart multipart=new MimeMultipart();
BodyPart msgBodyPart=new MimeBodyPart();
msgBodyPart.setContent("
Hi!
BodyPart embedImage=new MimeBodyPart();
DataSource ds=new URLDataSource(new URL("http://www.theoturnermedia.com/bulkemail/cri.JPG"));
// DataSource ds=new FileDataSource("C:/Documents and Settings/Anura/Desktop/New Folder (4)/opti.JPG");
embedImage.setDataHandler(new DataHandler(ds));
embedImage.setHeader("Content-ID","
// multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(embedImage);
message.setContent(multipart);
message.setSentDate(new Date());
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class ForcedAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sujeev@theoturnermedia.com","sujeev123");
}
}
}
thank you for complete code. but the problem is it shows compiler err.
ReplyDelete