sending email using smtp in java

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

Please visit the following link:

Java Mail API









Related Tutorials/Questions & Answers:
sending email using smtp in java
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.....!"); Transport transport = session.getTransport("smtp
java code to send email using gmail smtp server
java code to send email using gmail smtp server  please send me the java code to send email using gmail smtp server. and how to send verification code
Advertisements
code for sending email using j2me
code 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
send mail using smtp in java
send mail using smtp in java  How to send mail using smtp in java?   Sending mail in JSP - SMTP
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Email sending
Email 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
SMS sending questions using java - MobileApplications
using java language for sending SMS...SMS sending questions using java  am doing project sending SMS from... bulk SMS.am using mobile phone for sending SMS.is it possible sending bulk SMS
sending email code - JSP-Servlet
sending 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 authentication
Sending 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 and receiving xml message using Java Program
Sending 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 Program
Sending 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 automatic email - JavaMail
sending 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 email with read and delivery requests
Sending 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-Servlet
Email 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
automatically resend when connection lost in sending email
automatically resend when connection lost in sending email  send automatically email when connection lost in sending bulk email using javamail for exp more than 10000 emails
automatically resend when connection lost in sending email
automatically resend when connection lost in sending email  send automatically email when connection lost in sending bulk email using javamail for exp more than 10000 emails
automatically resend when connection lost in sending email
automatically resend when connection lost in sending email  send automatically email when connection lost in sending bulk email using javamail for exp more than 10000 emails
sending mail using jsp
sending 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
sending data to google chart api from mysql database using java
sending data to google chart api from mysql database using java  how to send data from mysql database to google chart api using java
ModuleNotFoundError: No module named 'py-smtp-email'
ModuleNotFoundError: No module named 'py-smtp-email'  Hi, My... 'py-smtp-email' How to remove the ModuleNotFoundError: No module named 'py-smtp-email' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py-smtp-email'
ModuleNotFoundError: No module named 'py-smtp-email'  Hi, My... 'py-smtp-email' How to remove the ModuleNotFoundError: No module named 'py-smtp-email' error? Thanks   Hi, In your python
How to send and recieve email by using java mailserver
How to send and recieve email by using java mailserver  How to send and receive email by using java mail server?   Hi Friend, Please visit the following link: Java Mail Thanks
sending data to google chart api from mysql database using java
sending data to google chart api from mysql database using java  Добовляйтесь в группу "МАЙНКРАФТА" Будьте участниками Группы Помогаите нам и себе Наша группа https://vk.com/club60851464 Есть свой сервер! Хорошая атмосфера
Sending data using bluetooth in android
Sending data using bluetooth in android  code to send data between two android mobile devices using bluetooth
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 Java
Sending 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 Java
Sending 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 Java
Sending 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
message sending and receiving using UDP TCP in J2ME
message sending and receiving using UDP TCP in J2ME   I need the simple program for message sending and receiving using UDP TCP in J2ME. Could u pls
how to send sms on mobile and email using java code
how 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 php
sending 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 images
java 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
Same email-massage is sending twice in online server(tomcat) but working fine in my local server(Tomcat)..
Same email-massage is sending twice in online server(tomcat) but working fine in my local server(Tomcat)..  Sir, I created one project in jsp for a software company. I am using listeners and timer for sending automatic
Email to multiple recipients using jsp
Email 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 jsp
Email 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-Servlet
sending mails   sending mail using smtp protocal ,while running,i got error an javax.mail.sendfailed exception. what is this error
sending sms with help of java - JavaMail
sending sms with help of java  hi can u plz help me to learn how to send sms with help of java. waiting for your reply
values of the "from address & to addresses" while sending a mail to localhost using javamail
values of the "from address & to addresses" while sending a mail to localhost using javamail  Hi, I verified javamail tutorial which is mentioned in JavaMail in ur website.I tried 1st program for sending the Mail.It's
SMS receive and sending throught PC using GSM modem
SMS receive and sending throught PC using GSM modem  How to send and receive SMS from PC using GSM modem Want code please could some one helpme
Sending SMS From Java Programs
Sending 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 mail
Sending 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 Email
Java 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

Ads