0

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 = "[email protected]";//change accordingly  
      String from = "[email protected]";
      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 [email protected] is not authorized.(WRONG SENDER MAILADDR)
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 550 [email protected] 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 [email protected] 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

5 Answers 5

2

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;        //"[email protected]" // 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>
Sign up to request clarification or add additional context in comments.

2 Comments

my query is i dont wqnt to use gmail smpt
this field is compulsary. you have to set redirecting path for your email to be sent. i see that you have specified @gmail.com in your code so i wrote smtp.gmail.com in given code.
1

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");

1 Comment

com.sun.mail.smtp.SMTPSendFailedException: 550 [email protected] is not authorized.(WRONG SENDER MAILADDR)
0

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 [email protected] 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.

Comments

0
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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.