Tôi đang cố gắng gửi thư cho bạn bè thông qua ứng dụng Java Mail của mình. Tôi có thể thực hiện thành công tuy nhiên cột của người nhận trong hộp thư hiển thị địa chỉ email đầy đủ thay vì tên của người gửi. Tôi đã thử thay đổi các thông số khác nhau nhưng vẫn hộp thư sẽ hiển thị địa chỉ e-mail đầy đủ thay vì tên của người gửi.Địa chỉ của người gửi thư Java được hiển thị thay vì tên của anh ấy
sử dụng phương pháp này để gửi thông điệp:
public void send(String key){
String to=key;
String from="mygmailid";
String subject="wassp";
String text="Hello";
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "myname");
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");
Session mailSession=Session.getDefaultInstance(props);
Message simpleMessage=new MimeMessage(mailSession);
InternetAddress fromAddress=null;
InternetAddress toAddress=null;
try{
fromAddress=new InternetAddress(from);
toAddress=new InternetAddress(to);
}
catch(AddressException e){
e.printStackTrace();
}
try{
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO,toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
transport.connect("smtp.gmail.com",465, "[email protected]", "mygmailpassword");
transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e){
e.printStackTrace();
}
}
Tôi gọi phương pháp này như:
public static void main(String[] args) {
MailSender mailer=new MailSender();
mailer.send("[email protected]");
}
Trong khi điều này về mặt lý thuyết có thể trả lời câu hỏi, nó không thực sự là một câu trả lời tốt, vì nó không dạy OP. Thay vào đó nó đưa ra một giải pháp thay thế mà không cần giải thích. Điều này thường sẽ dẫn đến OP không học hỏi, và quay trở lại để hỏi một câu hỏi mới khi một vấn đề tương tự xảy ra. Bạn có nhớ thêm một số lời giải thích? – Vogel612