How to set up the environment to use JavaMail?

I need to send a simple html message using JavaMail. And when I tried to find some nice examples with explanations on the Internet, each following example made me angry and angry.

All these silly examples contain copied and pasted Java code, which differs only in comments and a good disclaimer that you must first configure your SMTP server and pop3 server.

I understand that no one wants to advertise some specific products, but setting up a server is the hardest part. So, can someone give me really useful information (without java code) about setting up a specific server (for example, Kerio or any other)?

Now I have the following exception:

250 2.0.0 Reset state
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <mymail@mycompany.com> denied (authentication required)

UPD A simple reformulation of the entire previous text: Imagine that you have Windows, jdk and nothing more. And you want to make a java program and run it on your machine. And this program should send "Hello world!" to your gmail account. List your steps.

UPD2. Here is the code:

Properties props = new Properties ();
props.setProperty ("mail.transport.protocol", "smtp");
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty ("mail.user", "my_real_address_1@gmail.com");
props.setProperty ("mail.password", "password_from_email_above"); 

Session mailSession = Session.getDefaultInstance (props, null);
mailSession.setDebug (true);
Transport transport = mailSession.getTransport ();

MimeMessage message = new MimeMessage (mailSession);
message.setSubject ("HTML  mail with images");
message.setFrom (new InternetAddress ("my_real_address_1@gmail.com"));
message.setContent ("<h1>Hello world</h1>", "text/html");
message.addRecipient (Message.RecipientType.TO,
        new InternetAddress ("my_real_address_2@gmail.com"));

transport.connect ();
transport.sendMessage (message,
        message.getRecipients (Message.RecipientType.TO));

And an exception:

RSET
250 2.1.5 Flushed 3sm23455365fge.10
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 3sm23455365fge.10
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
    at com.teamdev.imgmail.MailSender.main(MailSender.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ...
+3
source share
4 answers

SMTP-, JavaMail. (Kerio, ... Exim, SendMail, Apache James, Postfix) Serverfault. SMTP- JavaMail.

"" . , Google Apps Google SMTP Java. Gmail ​​ , SMTP-, JavaMail.

MX Records SMTP- , , .

, , . , , .

Btw: , : SMTP . SMTP- (, ) example.com bob@example.net alice@example.org, SMTP . , , , . . : , (, example.com, , alice@example.com).

Edit:

, authenticationg ( Gmail, )

private Session createSmtpSession() {
  final Properties props = new Properties();
  props.setProperty("mail.smtp.host", "smtp.gmail.com");
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.smtp.port", "" + 587);
  props.setProperty("mail.smtp.starttls.enable", "true");
  // props.setProperty("mail.debug", "true");

  return Session.getDefaultInstance(props, new javax.mail.Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("john.doe@gmail.com", "mypassword");
    }
  });
}
+13

, , -1.1.jar mail-1.4.1.jar, SMTP - Gmail.

  • user@gmail.com user_pw return new PasswordAuthentication("user@gmail.com", "user_pw");

  • , myRecipientAddress@gmail.com , .

    package com.test.sendEmail;
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class sendEmailTest {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        sendEmailTest emailer = new sendEmailTest();
        //the domains of these email addresses should be valid,
        //or the example will fail:
        emailer.sendEmail();
    }
    
    /**
      * Send a single email.
      */
    public void sendEmail(){
    Session mailSession = createSmtpSession();
    mailSession.setDebug (true);
    
    try {
        Transport transport = mailSession.getTransport ();
    
        MimeMessage message = new MimeMessage (mailSession);
    
        message.setSubject ("HTML  mail with images");
        message.setFrom (new InternetAddress ("myJavaEmailSender@gmail.com"));
        message.setContent ("<h1>Hello world</h1>", "text/html");
        message.addRecipient (Message.RecipientType.TO, new InternetAddress ("myRecipientAddress@gmail.com"));
    
        transport.connect ();
        transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO));  
    }
    catch (MessagingException e) {
        System.err.println("Cannot Send email");
        e.printStackTrace();
    }
    }
    
    private Session createSmtpSession() {
    final Properties props = new Properties();
    props.setProperty ("mail.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.port", "" + 587);
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty ("mail.transport.protocol", "smtp");
    // props.setProperty("mail.debug", "true");
    
    return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("user@gmail.com", "user_pw");
      }
    });
    }
    
    }
    
+2

. .

SMTP-, (, , JavaMail), mymail@company.com. SMTP-. sfussenegger, javamail.

, , SMTP- SMTP-. , Thunderbird . Thunderbird, JavaMail .


Update:

SMTP- Google: smtp.gmail.com. , JavaMail? ?

+1

:

import java.text.MessageFormat;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
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 Emailer {

    public static void main(String[] args) {

        String hostname = args[0];
        final String userName = args[1];
        final String passWord = args[2];
        String toEmail = args[3];
        String fromEmail = args[4];
        String subject = args[5];
        String body = "";
        // add rest of args as one body text for convenience
        for (int i = 6; i < args.length; i++) {
            body += args[i] + " ";
        }

        Properties props = System.getProperties();
        props.put("mail.smtp.host", hostname);

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, passWord);
            }
        });

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(fromEmail));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);

        } catch (MessagingException e) {
            System.out.println("Cannot send email " + e);
        }
    }
}

JavaMail mail.jar javax.mail. , Google , . , , -?

0
source

Source: https://habr.com/ru/post/1726338/


All Articles