Question

Hi i am trying to send email through java code i am i have installed cmail server for sending email but i am not able to send email how can i send email

here is my code

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  

public class SendEmail  
{  
 public static void main(String [] args){  
      String to = "shaktisharma27789@gmail.com";//change accordingly  
      String from = "admin@shakti-pc.com";
      String host = "localhost";//or IP address  

     //Get the session object  
      Properties properties = System.getProperties();  
      properties.setProperty("mail.smtp.host", host);  
      Session session = Session.getDefaultInstance(properties);  

     //compose the message  
      try{  
         MimeMessage message = new MimeMessage(session);  
         message.setFrom(new InternetAddress(from));  
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
         message.setSubject("Ping");  
         message.setText("Hello, this is example of sending email  ");  

         // Send message  
         Transport.send(message);  
         System.out.println("message sent successfully....");  

      }catch (MessagingException mex) {mex.printStackTrace();}  
   }  
} 

when i run my program i am getting following Exception

com.sun.mail.smtp.SMTPSendFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at SendEmail.main(SendEmail.java:27)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
    ... 4 more

How can i achieve my output?

Thanks in advance

Was it helpful?

Solution

You have not given a password field in it. Additionally you have not specified your host. If you are sending email from local host, you should specify it. Also if you are sending mail by gmail server, you should use "smtp.gmail.com". Check http://www.tutorialspoint.com/servlets/servlets-sending-email.htm for clarifying your problem. From this tutorial you can send email with attachment too. And if you need code in jsp, I can provide you.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Simple Mail Program</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
<body>
<%@page import="java.sql.*"%>
<%@page import="javax.mail.*"%>

<%@page import="javax.mail.internet.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page import="java.math.BigInteger"%>



<%
        String host = "smtp.gmail.com";
    //host = smtp_server; //"smtp.gmail.com"; user = jsp_email;        //"YourEmailId@gmail.com" // email id to send the emails
//pass = jsp_email_pw; //Your gmail password
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to_add = request.getParameter("receiver");
String subject =request.getParameter("subject"); 
String messageText =request.getParameter("body"); 
String password = request.getParameter("pwd");
String from =request.getParameter("email_id");
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to_add) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); // use setText if you want to send text
Transport transport = mailSession.getTransport("smtp");
System.setProperty("javax.net.ssl.trustStore", "conf/jssecacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "admin");
transport.connect(host, from, password);
try 
{
    transport.sendMessage(msg, msg.getAllRecipients());
    out.println("sent");
    //WasEmailSent = true; // assume it was sent
}
catch (Exception err) 
{
    //WasEmailSent = false; // assume it's a fail
    out.println("Error" + err.getMessage());
}
transport.close();
%>
</body>
</html>

OTHER TIPS

You need to athenticate your email before sending add below code after setting properties ,

Authenticator authenticator = new Authenticator () {
    public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication("user" "password");
    }
};

and use

 Session session = Session.getDefaultInstance( props , authenticator);  

instead of

  Session session = Session.getDefaultInstance(properties);  

and if you dont want use authentication then set below property,

  properties.setProperty("mail.smtp.auth", "false");

The problem appears to be in the configuration of your mail server. Is your mail server running on the machine "shakti-pc.com"? If not, it's (rightly) preventing you from saying that your address is admin@shakti-pc.com so that you can't send email with a faked from address.

Also, unrelated to your current problem, you might want to fix these common mistakes in your program or in any code you've copied from others.

Use jdk 1.6 or 1.7 for sending emails.. JDk 1.8 throws exceptions frequently while sending mails.

Below I have pasted the sample code to send the mail

 public static void mail(){
      String host="HostName"
      final String user="SenderMailID"
      final String password="Password"
      final String senderName="senderName";
      final String subjectName="name"
      String[] to=getArrayOfEmails("ToEmail");
      String[] cc=getArrayOfEmails("CcEmail");
       Properties props = new Properties();
       props.put("mail.smtp.host",host);
       props.put("mail.smtp.auth", "true");
     //props.put("mail.smtp.starttls.enable", "true");
       Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user,password);
          }
        });
        try {
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(senderName + "<" + user + ">"));

         for(int i=0;i<to.length;i++)
         {
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to[i]));
        }
         for(int i=0;i<cc.length;i++)
         {
             message.addRecipient(Message.RecipientType.CC,new InternetAddress(cc[i]));
        }
         message.setSubject("subjName");
         BodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart.setText("bodyText");
         Multipart multipart = new MimeMultipart();
         multipart.addBodyPart(messageBodyPart);
         messageBodyPart = new MimeBodyPart();
         Transport.send(message);
         } catch (MessagingException e) {e.printStackTrace();}`enter code here`
     }

Test Automation

use ssl connection with port 465

To enable ssl set

props.put("mail.smtp.ssl.enable", true);

and to avoid exception 530 certification error add

MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top