我在下面的代码中使用java发送电子邮件。当我从日食发送它工作正常,但当我创建一个罐子并尝试发送电子邮件时,似乎阿拉伯字母不会去,有什么东西????????要去。请帮帮我。
package sa.com.medisys.send.mail;
public class SendMail {
String status = "Mail successfully sent." ;
public SendMail()
{
log("SendMail()");
}
public static void send(String SMTPhost,
String SMTPort,
String FromWithPass,
String To,
String Subject,
String Body,
String Attachment,
String UserName,
String Password ) throws SendMessageException
{
int StartPos = 0;
int EndPos;
char LastAttachChar;
String Filename;
String operation = null;
String[] processFrom = FromWithPass.split(",");
String from = processFrom[0];
UserName = from;
Password = processFrom[1];
String charset="UTF-8";
//Check if attachment is null
if (Attachment == null||Attachment.equals(""))
{
EndPos=0;
}
else
{
EndPos=Attachment.length();
LastAttachChar = Attachment.charAt(EndPos-1);
//If Attachment is terminated by comma, delete the last character from attachment
if (LastAttachChar==',')
{
Attachment = Attachment.substring(StartPos,EndPos-1);
EndPos=Attachment.length();
}
}
//Get properties and default session for the SMTP server
Properties props = System.getProperties();
props.put("mail.smtp.host", SMTPhost);
props.put("mail.smtp.port", SMTPort);
Session session = Session.getDefaultInstance(props, null);
//Now try and create the mail message
try
{
//Create a message
Message msg = new MimeMessage(session);
//Add sender to message
operation = "FROM";
msg.setFrom(new InternetAddress(from));
//Add reciepient list to message
operation = "TO";
InternetAddress[] address = InternetAddress.parse(To,false);
msg.setRecipients(Message.RecipientType.TO, address);
//Add subject to message
msg.setSubject(Subject);
//Add date to message
msg.setSentDate(new Date());
//Add a new part to the message
Multipart mp = new MimeMultipart();
{
System.out.println("BODY::" + Body);
MimeBodyPart mbp = new MimeBodyPart();
//Add the html body to the new part
mbp.setDataHandler(new DataHandler(new ByteArrayDataSource(new String(Body.getBytes(), "UTF-8"), "text/plain")));
mp.addBodyPart(mbp);
}
//Parse the attachment list and add files to the new part
if (EndPos != 0)
{
//Get the first and last positions of the file separator
int FilesepPos = Attachment.indexOf(",");
int FilesepLastPos = Attachment.lastIndexOf(",");
//The attachment contains only one file
if (FilesepPos==-1)
{
FilesepPos=EndPos;
EndPos=0;
}
while (true)
{
MimeBodyPart mbp = new MimeBodyPart();
Filename = Attachment.substring(StartPos,FilesepPos);
FileDataSource fds = new FileDataSource(Filename);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
if (EndPos==0)
{
break;
}
Attachment = Attachment.substring(FilesepPos+1,EndPos);
EndPos = Attachment.length();
if (FilesepPos==FilesepLastPos)
{
FilesepPos=EndPos;
EndPos=0;
}
else
{
FilesepPos = Attachment.indexOf(",");
FilesepLastPos = Attachment.lastIndexOf(",");
}
}
}
//Add the new part to the message
msg.setContent(mp);
//Send the message
if( UserName == null || Password == null)
Transport.send(msg);
else {
props.put("mail.smtp.auth", "true");
Transport tr = session.getTransport("smtp");
tr.connect(SMTPhost, UserName, Password);
msg.saveChanges();
tr.sendMessage(msg,msg.getAllRecipients());
tr.close();
System.out.println("Mail successfully Sent.");
}
}
catch (AddressException aex)
{
throw new Exception("Email Address error " + operation + " value > " + aex.getMessage());
}
catch (MessagingException mex)
{
Exception nested = mex.getNextException();
if(nested == null) nested = mex ;
throw new Exception(nested.getMessage());
}
catch (Exception ex)
{
throw new Exception(ex.toString());
}
}
void log( String sMessage )
{
System.out.println( sMessage ) ;
}
private String getBody() {
StringBuilder test = new StringBuilder();
try {
Scanner scanner = new Scanner(new FileInputStream("D:\\empty\\email.txt"), "UTF-8");
while (scanner.hasNextLine()) {
test.append(scanner.nextLine());
test.append(System.lineSeparator());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return test.toString();
}
public static void main(String[] args) {
String sub = "أخطاء المطبعية في عنوان خطاء";
String msg = new SendMail().getBody();
try {
new SendMail().send("mail.doamin.com", "25", "email@doamin.com,PASSWORD", "email@gmail.com", sub, msg, null, "", "");
System.out.println("Send");
} catch (Exception e) {
System.out.println("Send Failed");
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
Body.getBytes()
在JVM的默认字符集中获取字符串的字节数,因此您可能会丢弃那里的阿拉伯字符。
尝试:
new ByteArrayDataSource(Body, "text/plain")