sending email using smtp in java
Hi all,
I am trying to send and email to through my company mail server. Following is my code
package com.tbss;
import javax.mail.*;
import javax.mail.internet.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
class tester {
public String toMD5(String string){
byte[] defaultBytes = string.getBytes();
try{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(defaultBytes);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
String foo = messageDigest.toString();
System.out.println("string "+string+" md5 version is "+hexString.toString());
string=hexString+"";
}catch(NoSuchAlgorithmException nsae){
nsae.printStackTrace();
}
return string;
}
public static void main(String args[]) {
tester t=new tester();
String userName=t.toMD5("user");
String password=t.toMD5("password");
System.out.println("user name: "+userName);
System.out.println("password: "+password);
Properties props = new Properties();
props.put("mail.smtp.host" , "host");
props.put("mail.stmp.user" , userName);
//To use TLS
// props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", password);
//To use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.startssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
SmtpAuthenticator authenticator=new SmtpAuthenticator();
System.out.println("username: "+authenticator.getPasswordAuthentication().getUserName());
Session session = Session.getInstance( props , authenticator);
session.setDebug(true);
String to = "[email protected]";
String from = "[email protected]";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect( "host" , 25 , userName , password );
transport.send(msg);
System.out.println("fine!!");
} catch(Exception exc) {
System.out.println(exc);
}
}
static class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {
super();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
tester t=new tester();
String username = t.toMD5("user");
String password = t.toMD5("password");
if ((username != null) && (username.length() > 0) && (password != null)
&& (password.length () > 0)) {
return new PasswordAuthentication(username, password);
}
return null;
}
}
}
When i run this program i am getting following exception:
DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.tata-bss.com", port 25, isSSL false
220 IHGWTMEX07.SerWizSol.com Microsoft ESMTP MAIL Service ready at Wed, 21 Dec 2011 14:26:07 +0530
DEBUG SMTP: connected to host "host", port: 25
EHLO A5173423E.SerWizSol.com
250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: mechanism LOGIN not supported by server
DEBUG SMTP: mechanism PLAIN not supported by server
DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.tata-bss.com", port 25, isSSL false
220 IHGWTMEX07.SerWizSol.com Microsoft ESMTP MAIL Service ready at Wed, 21 Dec 2011 14:26:08 +0530
DEBUG SMTP: connected to host "mail.tata-bss.com", port: 25
EHLO A5173423E.SerWizSol.com
250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: mechanism LOGIN not supported by server
DEBUG SMTP: mechanism PLAIN not supported by server
DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
javax.mail.AuthenticationFailedException: 250-IHGWTMEX07.SerWizSol.com Hello [10.64.170.164]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
Please help me in resolving this problem.
Thanks,
Suresh
View Answers
December 21, 2011 at 3:37 PM
Related Tutorials/Questions & Answers:
sending email using smtp in javasending email using smtp in java Hi all,
I am trying to send and
email to through my company mail server. Following is my code
package com.tbss.....!");
Transport transport = session.getTransport("
smtp Advertisements
code for sending email using j2mecode for
sending email using j2me could someone tell me why when i try to compile this line
using netbeans 7.1
Properties property... for
sending a file attachment to gmail account
Email sendingEmail sending Hi sir,
Am doing a project, in that i need to send
email to multiple recipients at a same time
using jsp so send me the code as soon as possible.
Regards,
Santhosh
sending email code - JSP-Servletsending email code How To Send Emails
using jsp Hi friend,
I am
sending you a link. This link will help you.
Please visit for more information.
http://www.roseindia.net/mail/
sending-an-
email Sending email without authenticationSending email without authentication Hi sir, Am doing a project in JSP, in that i want to send mail without any authentication of password so send.../mail/
sending-an-
email-in-jsp.shtml
sending automatic email - JavaMailsending automatic email Dear sir.
In my project i need to send an automatic
email to the clients when their accounts are going to expire in 30 days.i am
using jsp,mysql and tomcat for my project.Expire information are stored
Sending and receiving xml message using Java ProgramSending and receiving xml message
using Java Program Hi Friends,
I want to send and receive xml files between two
java programs
using wire format,
could you suggest me the steps to be followed to acheive it or suggest some
Sending and receiving xml message using Java ProgramSending and receiving xml message
using Java Program Hi Friends,
I want to send and receive xml files between two
java programs
using wire format,
could you suggest me the steps to be followed to acheive it or suggest some
Sending email with read and delivery requestsSending email with read and delivery requests Hi there,
I am
sending emails
using JavaMail in Servlets on behalf of a customer from the website...
To read a mail, visit the following link:
Java Read Mail
Sending an email in JSP Sending an
email in JSP
Sending an
email in JSP
In this section, you will learn how to send an
email in
jsp.
Following is a simple JSP page for
sending Email sending in jsp - JSP-ServletEmail sending in jsp Hi,
Im trying to send
email from my jsp webserver.My webpage is running on Tomcat 5.0 server, and im
using jsp coding.Now i've already tried a sample code from google, for
sending email to my office '
smtp sending mail using jspsending mail
using jsp please give me the detailed procedure and code for
sending mail through jsp program
Please visit the following links:
http://www.roseindia.net/ejb/introduction/j2eedemo.shtml
http
Read Email using Java Mail API - James Read
Email using Java Mail API - James https://www.roseindia.net/javamail/send-mail.shtml
From this sample, i understand that we need to pass user credentials for reading the
email content.
Is there any option to read all
Read Email using Java Mail API - James Read
Email using Java Mail API - James https://www.roseindia.net/javamail/send-mail.shtml
From this sample, i understand that we need to pass user credentials for reading the
email content.
Is there any option to read all
Read Email using Java Mail API - James Read
Email using Java Mail API - James https://www.roseindia.net/javamail/send-mail.shtml
From this sample, i understand that we need to pass user credentials for reading the
email content.
Is there any option to read all
Read Email using Java Mail API - James Read
Email using Java Mail API - James https://www.roseindia.net/javamail/send-mail.shtml
From this sample, i understand that we need to pass user credentials for reading the
email content.
Is there any option to read all
Sending Emails In JavaSending Emails In Java I want to send emails from within a
java program. I saw some online
java programs to do that, but they needed me to enter a
smtp server, but i don't know what that should be. Can someone please help
Sending Emails In JavaSending Emails In Java I want to send emails from within a
java program. I saw some online
java programs to do that, but they needed me to enter a
smtp server, but i don't know what that should be. Can someone please help
Sending Emails In JavaSending Emails In Java I want to send emails from within a
java program. I saw some online
java programs to do that, but they needed me to enter a
smtp server, but i don't know what that should be. Can someone please help
how to send sms on mobile and email using java codehow to send sms on mobile and
email using java code hi....
I am developing a project where I need to send a confirmation/updation msg on clients mobile and also an
email on their particular
email id....plz help me to find
sending verification code to mobile using phpsending verification code to mobile
using php I had created a website and aim is to whoever the client register for site with their mobile number... the process and how to implement this
using php
Sending message using Java Mail
Sending message
using Java Mail
...
using javamail api. A client create new message by
using Message subclass...; // Set the RFC 822 "From" header field
using the
java mail sending with imagesjava mail
sending with images I need to send images through
java mail without giving content path(i.e. we don't want hard code the image path)can you tell me the idea?
Please visit the following links:
http
Sending Email with Attachment
Sending Email with Attachment
This Example shows you how to send a Attachment in the message
using
java mail api. A client create new message by
using Message subclass. It sets
Email to multiple recipients using jspEmail to multiple recipients
using jsp Hi sir,
Am a doing a project,in that i need to send
email to multiple user at a time,the to address should enter manually its not not be written in code
using jsp.
Regards,
Santhosh
Email to multiple recipients using jspEmail to multiple recipients
using jsp Hi sir,
Am a doing a project,in that i need to send
email to multiple user at a time,the to address should enter manually its not not be written in code
using jsp.
Regards,
Santhosh
sending mails - JSP-Servletsending mails
sending mail
using smtp protocal ,while running,i got error an javax.mail.sendfailed exception.
what is this error
Sending SMS From Java ProgramsSending SMS From
Java Programs I want to develop and application to send sms from my computer, can someone please help me, like tell me where to start and what i need
Sending images through java mailSending images through
java mail Am trying to develop greeting application that having images..... in one jsp page i displayed all images and by clicking one image the control go to mail
sending page in that the image should add
Java EmailJava Email I am making one
java email applications
using jsp-servlets. can you tell me that how can i recieve and send
email dynamically in my application in UI...
thanx.
Hi,ADS_TO_REPLACE_1
Please read at
Email email email hi
I am
using 1and1 server.
Using this server i am
sending a mail
using java program .
But it is running some problem occur
" Cannot send
email. javax.mail.SendFailedException: Invalid Addresses;ADS_TO_REPLACE_1