简体   繁体   中英

How to send email with non-english sender ID in Java?

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM