0
유로 기호 대신 첨부 파일에 다음 코드가있는 메일을 보내면이 "â,¬;"과 같은 이상한 문자가 나타납니다. 나는 OutputStreamWriter뿐만 아니라 이미 UTF-8로 메시지의 내용 유형을 설정했다. 이것에 대한 이유는 무엇일까요?자바 메일 첨부물에 이상한 유로화 기호
public static void sendMail(String email, String password, String sendTo, String subject, String msg)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
final String fEMail = email;
final String fPassword = password;
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(fEMail, fPassword);
}
});
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to mail address"));
message.setSubject(subject);
message.setContent("Hi", "text/plain; charset=UTF-8");
File file = File.createTempFile("test", ".csv");
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charset.forName("UTF-8").newEncoder());
PrintWriter writer = new PrintWriter(outputStreamWriter);
writer.println("\u20ac;");
writer.println("€");
writer.flush();
MimeMultipart rootMultipart = new MimeMultipart("mixed");
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDisposition(Part.ATTACHMENT);
bodyPart.setDataHandler(new DataHandler(new FileDataSource(file)));
bodyPart.setFileName(file.getName());
rootMultipart.addBodyPart(bodyPart);
message.setContent(rootMultipart);
Transport.send(message);
writer.close();
file.delete();
System.out.println("Message sent successfully!");
}
catch (MessagingException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
e.printStackTrace();
}
}
여전히 작동하지 않습니다. – eatSleepCode
Transport.send를 호출하기 바로 전에 message.writeTo (new FileOutputStream ("msg.txt"));를 추가하십시오. 그런 다음 msg.txt에 텍스트 부분에 적절한 문자 세트가 있는지 확인하십시오. 메시지를보기 위해 어떤 프로그램을 사용하고 있습니까? –
MS는 CSV로 탁월합니다. – eatSleepCode