1

次のコードを使用し、Javamail API を使用して電子メールを送信できます。これにより、「yahoo 電子メール ID」として「FROM」を使用して任意の電子メール ID に送信できます。コード:

mailFORM.html

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Mail form in html</title>
    </head>
    <body>
    <table border="1" width="50%"  cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%">
    <form method="POST" action="mail.jsp">
    <table border="1" width="100%" cellpadding="0" cellspacing="0">
    <h1>Mail API</h1>
    <tr>
    <td width="50%"><b>To:</b></td>
    <td width="50%"><input type="text" name="to" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>From:</b></td>
    <td width="50%"><input type="text" name="from" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>Subject:</b></td>
    <td width="50%"><input type="text" name="subject" size="30"></td>
    </tr>
    <tr>
    <td width="50%"><b>Description:</b></td>
    <td width="50%"><textarea name="description" type="text" 
cols="40" rows="15" size=100>
    </textarea>
    </td>
    </tr>
    <tr>
    <td><p><input type="submit" value="Send Mail" name="sendMail"></td>
    </tr>
    </table>
    </p>
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>  

メール.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
    javax.mail.internet.*,com.sun.mail.smtp.*"%>
    <html>
    <head>
    <title>sending mail using contactus form</title>
    </head>
    <body>
    <%
    try{
    Session mailSession = Session.getInstance(System.getProperties());
    Transport transport = new SMTPTransport(mailSession,new URLName("smtp.mail.yahoo.com"));
    transport = mailSession.getTransport("smtps");
    transport.connect("smtp.mail.yahoo.com",465,"myyahooid@yahoo.com","myyahoopassword");
   MimeMessage m = new MimeMessage(mailSession);
   m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
   Address[] toAddr = new InternetAddress[] {
   new InternetAddress(%><%request.getParameter("to")%><%)
   };
   m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
   m.setSubject(%><%request.getParameter("subject")%><%);
   m.setSentDate(new java.util.Date());
   m.setContent(%><%request.getParameter("description")%><%, "text/plain");

   transport.sendMessage(m,m.getAllRecipients());
   transport.close();
   out.println("Thanks for sending mail!"); 
   }
   catch(Exception e){
   out.println(e.getMessage());
   e.printStackTrace();
   }
   %>
   </body>  

ここで、簡単な CONTACTUS フォームを作成したいと思います。このフォームでは、明らかな理由から、"mailFORM.html" ファイルから "TO" フィールドを削除します。訪問者だけが私のウェブサイトにアクセスし、FROM、NAME、SUBJECT、および説明を入力して「mycompanyid@domain.com」に電子メールを送信するようにしたいからです。

では、ユーザー名とパスワードを入力するというこの問題を解決するにはどうすればよいでしょうか。ここにパスワードを入力したコードを作成できないためです。smtp ごとに個別のコードを作成することはできません...私の Web サイトの連絡先ページにアクセスしている VISITIR は、gmail、yahoo などの任意のドメインから電子メールを入力してフォームに入力できるためです。

結論として、「mycompanyid@domain.com」 http://www.tutorialspoint.com/about/contact_us.htmのような会社の特定の電子メールIDに詳細を送信するこのようなフォームを作成したいだけです(匿名のWebサイトを使用)

4

2 に答える 2

2

To email id を JSP ファイルにハードコーディングしますが、JSP で Java コードを記述しないようにすることをお勧めします。メソッド呼び出しのみを記述する必要があります。

private final String TO_EMAIL = "something@domain.com";

すべてのSMTPSで機能するコードを参照してください。

package com.naveed.workingfiles;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
 import org.apache.commons.mail.MultiPartEmail;

public class Mail {
String senderID;
String senderPassword;
String hostName;
int portNumber;
String attachmentPath;
String subject;
String body;
String cc;
String bcc;


// #=============================================================================================#
public String getBcc() {
    return bcc;
}

// #=============================================================================================#
public void setBcc(String bcc) {
    this.bcc = bcc;
}

// #=============================================================================================#
public String getCc() {
    return cc;
}

// #=============================================================================================#
public void setCc(String cc) {
    this.cc = cc;
}

// #=============================================================================================#
public String getBody() {
    return body;
}

// #=============================================================================================#
public void setBody(String body) {
    this.body = body;
}

// #=============================================================================================#
public String getSubject() {
    return subject;
}

// #=============================================================================================#
public void setSubject(String subject) {
    this.subject = subject;
}

// #=============================================================================================#

public String getSenderID() {
    return senderID;
}

// #=============================================================================================#
public void setSenderID(String senderID) {
    this.senderID = senderID;
}

public String getSenderPassword() {
    return senderPassword;
}

// #=============================================================================================#
public void setSenderPassword(String senderPassword) {
    this.senderPassword = senderPassword;
}

// #=============================================================================================#
public String getHostName() {
    return hostName;
}

// #=============================================================================================#
public void setHostName(String hostName) {
    this.hostName = hostName;
}

// #=============================================================================================#
public int getPortNumber() {
    return portNumber;
}

// #=============================================================================================#
public void setPortNumber(int portNumber) {
    this.portNumber = portNumber;
}

// #=============================================================================================#
public String getAttachmentPath() {
    return attachmentPath;
}

// #=============================================================================================#
public void setAttachmentPath(String attachmentPath) {
    this.attachmentPath = attachmentPath;
}

// #=============================================================================================#
public void sendMail(String receiverId) {

    try {
        // this below commented line for the HTML body text
        // MultiPartEmail htmlEmail = new HtmlEmail();
        // OR
        // HtmlEmail email = new HtmlEmail();

        MultiPartEmail email = new MultiPartEmail();
        // setting the port number
        email.setSmtpPort(getPortNumber());
        // authenticating the user
        email.setAuthenticator(new DefaultAuthenticator(getSenderID(),
                getSenderPassword()));
        // email.setDebug(true);
        email.setSSL(true);
        // setting the host name
        email.setHostName(getHostName());
        // setting the rciever id

        email.addTo(receiverId);

        // check for user enterd cc or not
        if (getCc() != null) {
            // add the cc
            email.addCc(getCc());
        }
        // check for user enterd bcc or not
        if (getBcc() != null) {
            // add the bcc
            email.addBcc(getBcc());
        }
        // setting the sender id
        email.setFrom(getSenderID());
        // setting the subject of mail
        email.setSubject(getSubject());
        // setting message body
        email.setMsg(getBody());
        // email.setHtmlMsg("<h1>"+getBody()+"</h1>");

        // checking for attachment attachment
        if (getAttachmentPath() != null) {
            // add the attachment
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath(getAttachmentPath());
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            email.attach(attachment);
        }

        // send the email
        email.send();
        // System.out.println("Mail sent!");
    } catch (Exception e) {
        // System.out.println("Exception :: " + e);
        e.printStackTrace();

    }
}// sendmail()
}// mail

ユーザーの電子メールセットのすべてのセッターに基づいて、それぞれのドメインの詳細 (ホスト名、ポート) をすべて保存する必要があるだけです。必要なすべてのライブラリをダウンロードします。

于 2013-07-06T19:02:38.633 に答える
1

Message.RecipientType.TO変数をハードコアすることができます。

どこかで静的変数を定義して、代わりにMY_MAIL使用できますMY_MAILrequest.getParameter("to")

ただし、必要なときにいつでも連絡できるように、送信者の電子メールをコードのどこかに必ず含めてください。

その他の考慮事項:

  • JSPページから実際のJavaコードを分離する方法について読んでください。Googleで十分なリンクを見つけることができます。
  • クラスをインポートしていますが、インポートを使用していません。
  • コードには役に立たないものがあります%><%。それらがどこにあるのかさえわかりません。
于 2013-07-06T18:36:49.327 に答える