Home Answers Viewqa JavaMail sending email using smtp in java

 
 


suresh anugandula
sending email using smtp in java
1 Answer(s)      a year and 5 months ago
Posted in : JavaMail

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 = "to@xxx.com";
  String from = "from@xxx.com";
  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 Pages:
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
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
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
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; nested exception
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
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
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
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
SMTP server
SMTP server  Hi How to configure SMTP server.how can send mail using java script ex:I am developing registration page, user register after how can i send mail automatically thanks venu
Mail from JSP with SMTP - JavaMail
Mail from JSP with SMTP  Hi, Can any one pls guide me how to send mail from JSp page by using SMTP IP address. Regards...,Britto.M  .../sending-an-email-in-jsp.shtml Hope that it will be helpful for you. Thanks
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
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 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
Java Email
Java Email  i am writting a program to send emails using gmail smtp server. I had the following error: java.lang.ClassFormatError: Absent Code...) Exception in thread "main" Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds
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
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
regarding email - JavaMail
to SMTP host: localhost, port: 25 ". please help me. thanks.   Hi friend, The main problem of "Can not connect to SMTP host: localhost... is using the default host name "localhost". Incorrect use
code for email - Spring
code for email  i want a java code using springs after login process sending an email to the corresponding with a text message to them as successfully registered  Hi Friend, Please visit the following links: http
Sending mail - JavaMail
emails using outlook client.From java program you can also send email.Here...) { to = newTo;}}Thanks  Java Code for sending messages(text or multimedia...Sending mail  Need a simple example of sending mail in Java 
Email - Electronic mail
messages. E-mail are sent, stored, delivered and fetched using standard SMTP and POP... of your emails, virus detection and removal from email, POP and SMTP access...Email - Electronic mail In this article we will understand the E-mail and see
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 mails - JSP-Servlet
sending mails   sending mail using smtp protocal ,while running,i got error an javax.mail.sendfailed exception. what is this error
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, Please read at Email From JSP &
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
how to establiish an smtp server ?
how to establiish an smtp server ?  we have an need of establishing an smtp server . "our scenario is that first when the customer enters his... on his email id we have to send him an confirmation mail" . we are haVING
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
PHP Email Tutorial, Sending email from PHP
Sending mail in PHP In this section we will learn how to send email from PHP program. You can send email from your PHP script using mail(...) function... call. Example 2. $email="admin@yourdomain.com, admin@otherdomain.com
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
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
java code to send an email
java code to send an email  i developed one java code that has to send a mail but i am getting an runtime exception calledjavax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested
email extractor - Java Beginners
email extractor  how to extract only email address from a lines... language? I have the idea ,copying one file text to another file by using buffer...; Sorry! I am a java developer.It's been long time that i left 'C
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
How does Email works
to deliver Email to the recipient mail server. Java mail Application communicates... How does Email works       Each Internet domain has email Server, when a user sends
generate Email - JSP-Servlet
generate Email  Hi I need to generate an automated email and need to send it to the client like an confirmation email after registering to my website. I m using jsp code. Please help me out with this issue.  Hi friend
email entire page javascript
email entire page javascript  How to email entire page in JavaScript without using OB?   ob_end_start(); // page content if(!empty($_GET['purpose']) && $_GET['purpose'] == "email") { $message = ob_get
sending doc files - Swing AWT
sending doc files  how to send doc file using java from one machine to other over a network
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 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
Email validation is JSP using JavaScript
Email validation is JSP using JavaScript... will show you how to validate email address in you JSP program using JavaScript... will do the email validation using the function emailcheck(). The funcation
Send Email From JSP & Servlet
(java servlet development kit).  (We are using Tomcat server. ... J2EE Tutorial - Send Email From JSP & Servlet...; -------------------------------------------------------------------------------------------------------------------------- As for sending
sending mail with attachment in jsp - JSP-Servlet
sending mail with attachment in jsp  Hi, Can any one pls tell me how to send email with attachment in jsp. I know how to send mail without... wrking 4 me... Its throwing an error "Could not connect to SMTP host: localhost
Digitally signing email through Java mail api
Digitally signing email through Java mail api  how to sign (Digital Signature) one message using Java mail api
Email Validation code - Java Interview Questions
Email Validation code  Can anybody tell me how to write email validation code using java   Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java/core/regularExpressions.html Thanks
Validate Domain of an Email Id - JSP-Servlet
Validate Domain of an Email Id  How to validate the domain name in an Email id before sending a mail for an application using Mail API. ex: a@a.com in the above email id i want to validate whether a.com is present
how to send contact detail in email
%"><b>Email:</b></td> <td width="100%"><input type="text" name="email" size="50"></td> </tr> <tr>...;/html> and this is my jsp page.... <%@ page language="java
JSP Email
JSP Email  Hi, How to send email using JSP? Thanks   Hi, Check this tutorial: Send Email to selected dropdown user Thanks

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.