setFrom()メソッドをどのように変更したいのですか? Gmailの添付ファイルを使って電子メールを送信し、setFromテキストを変更することができますが、メールにはusername
が表示されます。私もyahooアカウントを使ってみましたが、認証エラーが出ます。Javamail API - setFromをどのように変更しますか?
送信元アドレスを変更したいと思います。コードは以下の通りである:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "[email protected]";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Googleのリレーを使用しないでください。 – Perception
私の友人、あなたのメールを送信するためにGoogleリレーを使用しています( 'props.put(" mail.smtp.host "、" smtp.gmail.com ");')。あなたのユーザ名が発信元アドレスとして表示されることをあまり厳しくしない場合は、別のリレーを使用してください。 – Perception
あなたがやろうとしていることは、一般的に「スパム」と呼ばれています。 :-)だからこそGmailはあなたにそれをさせません。正当な理由がある場合は、次のGmailのヘルプページをご覧ください:[別のアドレスからメールを送信](https://support.google.com/mail/bin/answer.py?hl=ja&answer=22370) –