Advertisement
Advertisement
| 06.20.2008 at 07:08AM PDT, ID: 23502215 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: |
package org.me.mail.Test;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.*;
public class MailOnGmailnSSL
{
String d_email = "xxxxx@gmail.com",
d_password = "xxxx",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "xxxx@companyName.com",
m_subject = "Testing",
m_text = "Hey, this is the test mail.";
public static final String TXT_FILE = "c:\\temp\\text_contents.txt";
public static final String HTML_FILE = "c:\\temp\\html_contents.html";
public static final String IMAGE_FILE = "c:\\temp\\ch851118.gif";
public MailOnGmailnSSL()
{
Properties props = new Properties();
props.setProperty("proxySet","true");
props.setProperty("socksProxyHost","proxy.sgp.hp.com");
props.setProperty("socksProxyPort","8080");
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
Multipart mp = new MimeMultipart("alternative");
BodyPart bp = getFileBodyPart(TXT_FILE);
mp.addBodyPart(bp);
//msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Multipart mp_html = new MimeMultipart("related");
BodyPart bp_html = getFileBodyPart(HTML_FILE);
mp_html.addBodyPart(bp_html);
String cid = "cid:image";
MimeBodyPart mp_image = new MimeBodyPart();
FileDataSource fd = new FileDataSource(IMAGE_FILE);
mp_image.setFileName(IMAGE_FILE);
mp_image.setText("Image 1");
mp_image.setDataHandler(new DataHandler(fd));
mp_image.setHeader("Content_id", "<"+cid+">");
mp_image.setDisposition("inline");
mp_html.addBodyPart(mp_image);
BodyPart bp2 = new MimeBodyPart();
bp2.setContent(mp_html);
mp.addBodyPart(bp2);
msg.setContent(mp);
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
private BodyPart getFileBodyPart(String txt_file) throws javax.mail.MessagingException {
BodyPart bp = new MimeBodyPart();
bp.setDataHandler(new DataHandler(new FileDataSource(txt_file)));
return bp;
}
public static void main(String[] args)
{
MailOnGmailnSSL blah = new MailOnGmailnSSL();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}
|