0

Java アプリケーションで単純なメールを送信しようとしていますが、メールの送信を実行すると、コンソールに「220 2.0.0 TLS を開始する準備ができました」とだけ表示され、後で何も起こりません。メソッドコードは次のとおりです。

try
    {
        /** Recipient address **/
        String messageRecipient = "@live.com";

        String pass = "";
        /** sender address **/
        String messageSender = "@gmail.com";

        /** get my properties **/
        Properties properties = System.getProperties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.user", messageSender);
        properties.put("mail.smtp.password",  pass);
        properties.put("mail.debug", "true");
        properties.put("mail.smtp.debug", "true");
        properties.put("mail.smtp.port", "587");

        /** get default session object **/
        Session session = Session.getDefaultInstance(properties,// null);
                new javax.mail.Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("@gmail.com", "");
            }
        });

        try
        {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(messageSender));

            //set to: header field of the header
            message.addRecipient(Message.RecipientType.TO, 
                            new InternetAddress(messageRecipient));
            //set subject: header field
            message.setSubject("Welcome to java mail!");

            //send message
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", 587, messageSender, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            System.out.println(Errors.MESSAGE_SENT_SUCCESSFULLY);
            return null;
        }
        catch(MessagingException mex)
        {
            return Errors.MESSAGE_NOT_SENT;
        }
    }
    catch(Exception e)
    {
        return Errors.FILE_NOT_FOUND;
    }
}

コンソールで出力をデバッグします。

     DEBUG: JavaMail version 1.4.5
    DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
    DEBUG: Tables of loaded providers
    DEBUG: Providers Listed By Class Name:         {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSS        LTransport,Sun Microsystems, Inc],                         com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTranspo        rt,Sun Microsystems, Inc],         com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Su        n Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Su        n Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun                 Microsystems, Inc],         com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun         Microsystems, Inc]}
    DEBUG: Providers Listed By Protocol:         {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc],         imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc],         smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun                 Microsystems, Inc],         pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun         Microsystems, Inc],         pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun         Microsystems, Inc],         smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun                 Microsystems, Inc]}
    DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
    DEBUG: getProvider() returning         javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
    220 mx.google.com ESMTP e20sm6680361wiv.7
    DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

    EHLO gadon-Komputer
    250-mx.google.com at your service, [83.22.91.238]
    250-SIZE 35882577
    250-8BITMIME
    250-STARTTLS
    250 ENHANCEDSTATUSCODES
    DEBUG SMTP: Found extension "SIZE", arg "35882577"
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    STARTTLS
    220 2.0.0 Ready to start TLS
    DEBUG: getProvider() returning         javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
    220 mx.google.com ESMTP et10sm6688757wib.2
    DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

    EHLO gadon-Komputer
    250-mx.google.com at your service, [83.22.91.238]
    250-SIZE 35882577
    250-8BITMIME
    250-STARTTLS
    250 ENHANCEDSTATUSCODES
    DEBUG SMTP: Found extension "SIZE", arg "35882577"
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    STARTTLS
    220 2.0.0 Ready to start TLS

誰かがこれの何が問題なのか教えてもらえますか? javamail に関する何百ものトピックを読みましたが、私の問題に対する答えが見つかりませんでした.パスワードを変更しても、出力は何も変わりません。

4

5 に答える 5

2

JavaMail FAQの Gmail の例を使用して、コードを簡素化できます。(ポート番号を設定せず、「smtps」プロトコルを使用し、Authenticator を取り除きます。) また、getDefaultInstance を getInstance に変更します。

于 2012-06-08T17:19:00.627 に答える
0

以下のコードは、Javaを使用したEメールの単純な実装です。JavaメールAPIのドキュメントを参照してこれを作成しましたが、完全に機能します。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;

public class Email
{
public Email()
    {
try {
    String host = "smtp.gmail.com";
    String username = "youmailid@gmail.com";
    String password = "XXXXXX";
    String body = "This is the message body.";
    String subject = "This is the message subject."; 
    //Set the properties
    Properties props = new Properties();
    props.put("mail.smtps.auth", "true");
    // Set the session here
    Session session = Session.getDefaultInstance(props);
    MimeMessage msg = new MimeMessage(session);
    // set the message content here
      msg.setSubject(subject);
      msg.setText(body);
      msg.setFrom(new InternetAddress(username));
      msg.addRecipient(Message.RecipientType.TO, 
              new InternetAddress(username));
    Transport t = session.getTransport("smtps");

    t.connect(host, username, password);
    t.sendMessage(msg, msg.getAllRecipients());
        t.close();
    }
   catch (Exception e) {
   e.printStackTrace();
   }
 finally {

    }
     }
    public static void main(String[] args)
    throws Exception {
            new Email();
    }

}
于 2012-12-26T17:30:45.813 に答える
0

そうでない場合は、Spring を使用して電子メールの送信プロセスを簡素化することをお勧めします。

簡単なメールを送信するには、次のように書く必要があります。

 `import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.mail.MailException;
  import org.springframework.mail.MailSender;
  import org.springframework.mail.SimpleMailMessage;`
.........
@Autowired
private MailSender mailSender;

SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo("example@gmail.com");
            mailMessage.setFrom("myemail@gmail.com");
            mailMessage.setSubject(this.subject);
            mailMessage.setText(text.toString());
            try {
                this.mailSender.send(mailMessage);
                logger.info("Email inviata con successo");
            }
            catch (MailException ex) {
                // just log it and go on
                logger.warn("An exception occured when trying to send email", ex);
            }

そしてあなたのapplicationContext.xmlで

 <!-- MailSender used by EmailAdvice -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"/>
        <property name="port" value="${mail.port}" />
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />

        <property name="javaMailProperties">
           <props>
                  <prop key="mail.smtp.auth">true</prop>
                  <prop key="mail.smtp.starttls.enable">true</prop>
               </props>
        </property>
    </bean>

詳細については、この簡単なチュートリアルに従ってください http://www.mkyong.com/spring/spring-sending-e-mail-via-gmail-smtp-server-with-mailsender/

于 2012-06-08T09:05:22.963 に答える
0

を削除してみてproperties.put("mail.smtp.starttls.enable", "true")、ポートを 465 に変更し、次の 2 つのプロパティを追加します。

props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
于 2012-06-08T09:59:20.660 に答える