3

JavaMail で単純な html メッセージを送信する必要があります。そして、インターネットで説明付きの良い例を見つけようとしたとき、次の例ごとに私はますます腹を立てました.

これらのばかげた例にはすべて、コメントのみが異なるコピー アンド ペーストされた Java コードが含まれています。

誰も具体的な製品の宣伝をしたくないことは理解していますが、サーバーの構成が最も難しい部分です。それで、具体的なサーバー(たとえば、Kerio、または他のサーバー)の構成に関する本当に役立つ情報(Javaコードなし)を誰か教えてもらえますか?

私が今持っているのは次の例外です:

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)

アップデート。これまでのすべてのテキストを単純に再構成すると、Windows と jdk しかないと想像してください。そして、Java プログラムを作成してマシン上で実行したいとします。そして、このプログラムは「Hello world!」を送信する必要があります。あなたのGmailアカウントに。手順をリストします。

UPD2. コードは次のとおりです。

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));

例外は次のとおりです。

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)
    ...
4

4 に答える 4

13

SMTPサーバーを構成するためのチュートリアルを探している場合は、JavaMailを探すべきではありません。選択したサーバー(Kerio、たとえば...またはEximSendMailApache JamesPostfix)でチュートリアルを探すか、 Serverfaultで質問してください。SMTP準拠のサーバーは、JavaMailとうまく連携します。

または、「標準」のメールプロバイダーのインフラストラクチャを使用することもできます。たとえば、私はGoogle AppsアカウントとGoogleのSMTPインフラストラクチャを使用して、Javaアプリケーションからメールを送信しています。とにかく、JavaMailをテストドライブするために独自のSMTPサーバーをセットアップしたくない場合は、Gmailアカウントを使用することをお勧めします。

最後のオプションとして、ドメインのMXレコードを検索し、受信者のSMTPサーバーにメールを直接配信することもできます。困難を回避するための一般的な落とし穴がいくつかあります。

最後のポイントとして、メールがスパムとしてフィルタリングされないようにする方法を検討する必要があります。これは、それ自体が大きなトピックです。ここでは、独自のサーバーをホストするときに発生する可能性のある問題のいくつかに対処する標準プロバイダーに依存することが役立ちます。

ところで:あなたが投稿したエラーメッセージに関して:SMTPサーバーはメッセージの中継を拒否しています。これは、SMTPサーバー(それを考えている)がexample.comで実行されていて、bob @ example.netとしてalice@example.orgに送信している場合、SMTPサーバーにリレーとして機能するように要求している場合です。これは数年前の一般的な慣習でしたが、ご想像のとおり、スパマーに悪用されていました。当時から、郵便局長は中継を拒否することが奨励されています。メールを送信する前に認証するか、サーバーでのみホストされているアカウント(example.com、例:alice@example.com)に送信するかの2つの選択肢があります。

編集:

これは、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");
    }
  });
}
于 2009-12-22T12:26:44.180 に答える
1

私はあなたの問題の一部を見ることができます。エラーメッセージで適切に説明されています。

メールの送信先のSMTPサーバー(つまり、JavaMail構成で構成したアドレスの1つ)は、へのメールの転送を拒否していmymail@company.comます。SMTPサーバーの構成の問題のようです。sfusseneggerが示したように、それはjavamailとは何の関係もありません。

したがって、すべての面で同時にデバッグしているわけではありません。動作している既知のSMTPクライアントからSMTPサーバーをアドレス指定してみることをお勧めします。たとえば、サンダーバードはうまくいくでしょう。Thunderbirdからメールを送信できれば、JavaMailからの問題はほとんどないはずです。


アップデート:

GoogleのSMTPサーバーの正しいアドレスは次のとおりsmtp.gmail.comです。これは、JavaMailで構成したサーバーですか?一致するエラーメッセージを表示できますか?

于 2009-12-22T12:29:44.587 に答える
1

activation-1.1.jarmail-1.4.1.jarを使用し、SMTP ホストがGmailである上記の回答を組み合わせた実際の例。

  1. 置き換えuser@gmail.comuser_pw並べるreturn new PasswordAuthentication("user@gmail.com", "user_pw");

  2. 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");
      }
    });
    }
    
    }
    
于 2011-10-10T09:32:41.070 に答える
0

これはうまくいくはずです:

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);
        }
    }
}

javax.mail の依存関係のために、JavaMail の mail.jar をクラスパスに配置する必要があります。Google がユーザーの思い通りにメールを送信できるかどうかはわかりません。ご利用の ISP など、別のメール プロバイダーを試してみませんか?

于 2009-12-22T13:21:14.297 に答える