我想从非英文电子邮件ID(例如डेमो@डेमो.कॉम)发送电子邮件至任何电子邮件ID 使用java。
当我使用时:
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();
}
它抛出异常,如:
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)
请告诉我我需要做些什么。
答案 0 :(得分:0)
您应该做的是添加编码格式:
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();
}
当您在.setText
方法中设置编码时,您也会告诉标头编码。
答案 1 :(得分:0)
首先,你需要一个完整的地址(包括域名部分)。如果您的域还包含国际字符,则需要进行转换 使用java.net.IDN.toASCII这样的Punycode:
String[] emailSplit = from.split("@");
String local = emailSplit[0];
String domain = emailSplit[1];
String punycodeDomain = java.net.IDN.toASCII(domain);
String newEmail = local + "@" + punycodeDomain;
(简化版,需要进行一些错误检查)