2

I want to send email from non-english email ID like डेमो@डेमो.कॉम to any email ID using java.

When I use :

String to = "demo@gmail.com";
 String from = "डेमो@डेमो.कॉम";

      String host = "localhost";

      Properties properties = System.getProperties();
          properties.setProperty("mail.smtp.host", host);

      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from));


         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }

It throw exception like :

javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: 501 Syntax error in parameters or arguments

    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at com.data.TestingSendMail.main(TestingSendMail.java:49)

Please suggest me what I need do for it.

Cin Sb Sangpi
  • 687
  • 3
  • 7
  • 22
Tell Me How
  • 672
  • 12
  • 16

2 Answers2

0

What you should do is add the encoding format:

String to = "demo@gmail.com";
String from = "डेमो@डेमो.कॉम";

String host = "localhost";

Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);

Session session = Session.getDefaultInstance(properties);

try{
   // Create a default MimeMessage object.
   MimeMessage message = new MimeMessage(session);

   message.setFrom(new InternetAddress(from));


   message.addRecipient(Message.RecipientType.TO,
                            new InternetAddress(to));

   message.setSubject("This is the Subject Line!");

   message.setText("This is actual message","UTF-8");

   Transport.send(message);
   System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
   mex.printStackTrace();
}

When you set an encode in the .setText method you also tell the header encoding.

Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
  • You could also check [this post](http://stackoverflow.com/questions/15044027/utf-8-charset-doesnt-work-with-javax-mail) and this [other post](http://www.coderanch.com/t/603839/java/java/sending-english-characters-Java-Mail) – Jonathan Solorzano Sep 30 '15 at 05:55
  • I know the UTF-8 working for setText() but it used for non-english body message. And my problem is that sender email id is in hindi or any other non-english language. – Tell Me How Sep 30 '15 at 06:05
  • If you're interested in testing a version of JavaMail 1.6.0 with this support, please contact me at javamail_ww@oracle.com. – Bill Shannon Dec 08 '16 at 00:27
0

First you need a complete from adress (including domain part). If your domain also contains international characters, you need to convert it to Punycode using java.net.IDN.toASCII like this:

String[] emailSplit = from.split("@");
String local = emailSplit[0];
String domain = emailSplit[1];
String punycodeDomain = java.net.IDN.toASCII(domain);
String newEmail = local + "@" + punycodeDomain;

(simplified version, some error checking required)

Drunix
  • 3,313
  • 8
  • 28
  • 50
  • it showing same error.:501 Syntax error in parameters or arguments – Tell Me How Sep 30 '15 at 06:09
  • Taking a closer look your from adress is not complete, it does not contain an "@" char. My answer assumed that you use international domain names. – Drunix Sep 30 '15 at 06:12