In general, the situation is this, I am sending letters via javax.mail, I’ll give you a piece of code:

Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromMail)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail)); msg.setSubject(Subject); MimeBodyPart msgPart = new MimeBodyPart(); MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(msgPart); msgPart.setText(bodyMail, "UTF-8"); msgPart.setHeader("Content-Type", "text/html; charset=\"utf-8\""); msg.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(smtp_host, smtp_port, smtp_user, smtp_pass); msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients()); 

The question is how do I substitute the sender's name in the FROM field, i.e. that was

FROM: Vasya Pupkin pupkin@mail.com

I can not figure out that setFrom () can have an argument only InternetAddress :( Can it be somehow through setHeader ()?

    1 answer 1

    In general, I decided this question, it was necessary to do:

     msg.setFrom(new InternetAddress(fromMail, fromName)); 

    I rummaged in the designers InternetAddress () and found :) Maybe someone will be useful.

    • Such a question was already here on a hashcode .. half a year ago somewhere. Here it is: hashcode.ru/questions/7802 - cy6erGn0m
    • one
      Generally there the question about the coding was set. However, two days of searching in Google and the solution was found on stackoverflow.com :) - Shamanis
    • And in javadoc in the description of the constructor InternetAddress () this was not? - avp
    • @Shamanis yes, but the question and the answer contained the answer to your question and even in a more difficult case. @avp documentation from nivichkov read not accepted (and even some categories of developers). - cy6erGn0m
    • one
      @ cy6erGn0m do not need to be rude here only. All the code was written entirely in the documentation and I read it quite a bit, but with this case I was slightly confused because the setFrom () method began to look, only then it dawned on me that the InternetAddress () designer plays a key role. I do not have much experience, but such resources are created to share this experience :) - Shamanis