2

次のコードを使用してメールを送信しています。

protected void mailSettings() {
    Object lookedUp = null;
    String mailSettingValues[] = null;
    try {
        InitialContext initialContext = new InitialContext();
        lookedUp = initialContext.lookup("java:/comp/env/mailsettings");
        mailSettingValues = lookedUp.toString().split(",");
        smtphost = mailSettingValues[0];
        port = mailSettingValues[1];
        username = mailSettingValues[2];
        password = mailSettingValues[3];
    } catch (NamingException e) {
        e.printStackTrace();
    }

    m_properties = new Properties();
    m_properties.put("mail.smtp.host", smtphost);
    m_properties.put("mail.smtp.auth", "true");
    m_properties.put("mail.smtp.starttls.enable", "true");
    m_properties.put("mail.smtp.port", port);

    m_Session = Session.getDefaultInstance(m_properties,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

}

public void sendMail(String mail_from, String mail_to, String mail_subject,
        String m_body) {
    try {
        m_simpleMessage = new MimeMessage(m_Session);
        m_fromAddress = new InternetAddress(mail_from);
        m_toAddress = new InternetAddress(mail_to);

        m_simpleMessage.setFrom(m_fromAddress);
        m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
        m_simpleMessage.setSubject(mail_subject);

        m_simpleMessage.setContent(m_body, "text/plain");

        Transport.send(m_simpleMessage);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

to_emailこのコードの時点で、アドレスからの電子メールとそのパスワードが必要ですが、認証(ユーザーパスワードの取得)なしでアドレスにメールを送信したいと思います。

これはどのように行うことができますか?

私は次のことをする必要があります:

  1. プロパティまたはコンテキストファイルから差出人アドレスを取得します
  2. プロパティまたはコンテキストファイルから宛先アドレスを取得します
  3. to_address差出人アドレスを認証せずににメールを送信する
4

3 に答える 3

4
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SimpleSendEmail {
    public static void main(String[] args) {

        String host = "your smtp host";
        String to = "bbbb@ddddd.com";
        String from = "xxxx@yyy.com";
        String subject = "My First Email";
        String messageText = "I am sending a message using the"
                + " JavaMail API.\n" + "Here type your message.";
        boolean sessionDebug = false;
        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        Session session = Session.getDefaultInstance(props, null);
        // Set debug on the Session so we can see what is going on
        // Passing false will not echo debug info, and passing true
        // will.
        session.setDebug(sessionDebug);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = { new InternetAddress(to) };
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);

            Transport.send(msg);
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}
于 2012-07-30T12:25:44.100 に答える
2

一般的な意味では、それは不可能です。

電子メールを送信できるようにするには、SMTPサーバーが必要です。したがって、SMTP接続を確立するには、このサーバーの詳細を知る必要があります。少なくとも、ホスト名(およびポートですが、これは通常、デフォルト値の25と見なすことができます)。

その場合、SMTPサーバー自体がおそらく認証を必要とします。その場合、それを幸せに保つために十分な情報を利用できるようにする必要があります-おそらくプリンシパル(/ユーザー名)とクレジット(/パスワード)。

匿名リレーを許可するSMTPサーバーを見つけた場合、つまり、最初に認証せずにメールを送信できる場合は、もちろん、認証手順をスキップでき、パスワードは必要ありません。

ただし、送信を行うにはSMTPサーバーが必要です。そのため、アドレスの「単なる」ペアで電子メールを送信することはできません。

于 2012-07-30T11:49:06.810 に答える
1

パブリック匿名リレーを許可するSMTPサーバーはスパムのソースとしてブラックリストに登録されるため、SMTP認証を使用することをお勧めします。

リレーを許可する独自のローカルSMTPサーバーを実行するか、使用しているアプリケーションサーバーに応じて、JNDIに認証セッションを配置してそれらを検索するようにサーバーを構成できる場合があります。ユーザー名とパスワードをコーディングする必要はありませんが、それでもアプリサーバーに提供する必要があります。

于 2012-07-30T17:23:33.443 に答える