send email with SMTP command, using socket programming in Java
I am trying to send email via socket programming using SMTP commands using socket in java. But i am unable to do so. The problem is in authentication may be.
I need SMTP commands to send email and to authenticate user over server.
Any help appreciated.
Thanks 开发者_开发百科in Advance Imran
Create Transport Object
Properties props = new Properties();
props.setProperty("mail.transport.protocol", pProtocol);
props.setProperty("mail.host", pHost);
props.setProperty("mail.user", pUser);
props.setProperty("mail.password", pPassword);
mMailSession = Session.getDefaultInstance(props, null);
mMailSession.setDebug(true);
try {
mTransport = mMailSession.getTransport();
mTransport.connect();
} catch (MessagingException e) {
mLog.error(e.getMessage(), e);
throw new MailException(e);
}
Send mail
try {
MimeMessage message = new MimeMessage(mMailSession);
message.setSubject(pSubject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(pContent, "text/html");
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
message.setContent(mp);
message.addFrom(new Address[] { new InternetAddress(pFrom) });
for (int i = 0; i < pTo.length; i++) {
String tTo = pTo[i];
message.addRecipient(Message.RecipientType.TO, new InternetAddress(tTo));
}
mTransport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
} catch (MessagingException me) {
throw new MailException(me);
}
EDIT:
After the comment from Thilo, i would like to append, that this solution depend on com.sun.mail and imports javax.mail.* classes.
精彩评论