Javamail API - How do you change setFrom to whatever you want?

后端 未结 4 1678
孤独总比滥情好
孤独总比滥情好 2021-01-12 06:38

How do I change the setFrom() method to whatever I want? I can send e-mails through my gmail accoutn and change the setFrom text, but it shows my username for t

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 06:55

    (Assuming I understand your goal and it's not to send spam) The Apache Commons library we use does it by calling setPersonal on the InternetAddress. http://docs.oracle.com/javaee/7/api/javax/mail/internet/InternetAddress.html#setPersonal-java.lang.String-java.lang.String-

            Message message = new MimeMessage(session);
            InternetAddress me = new InternetAddress("from-email@gmail.com");
            me.setPersonal("My name");
            message.setFrom(me);
    

提交回复
热议问题