gpt4 book ai didi

java - java中的阿拉伯字符发送电子邮件问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:18 26 4
gpt4 key购买 nike

我使用下面的代码使用java发送电子邮件。当我从 eclipse 发送时,它工作正常,但是当我创建一个 jar 并尝试发送电子邮件时,似乎阿拉伯字母不会出现,有一些东西?????????要去。请帮助我。

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

最佳答案

Body.getBytes() 获取字符串的字节,无论您的 JVM 的默认字符集是什么,因此您可能会丢弃其中的阿拉伯字符。

尝试:

new ByteArrayDataSource(Body, "text/plain")

关于java - java中的阿拉伯字符发送电子邮件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com