1

我可以使用以下代码和使用 Javamail API 发送电子邮件,通过它我可以使用“FROM”作为我的“雅虎电子邮件 ID”发送到任何电子邮件 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 创建单独的代码...因为访问我的网站联系页面的 VISITIR 可以通过输入来自任何域(即 gmail、yahoo 等)的电子邮件来填写表格。

总而言之,我只想创建这样的表单(使用任何匿名网站),它将详细信息发送到公司的特定电子邮件 ID,例如“mycompanyid@domain.com”
http://www.tutorialspoint.com/about/contact_us.htm

4

2 回答 2

2

在 JSP 文件中硬编码您的电子邮件 ID,但避免在 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_MAIL而不是request.getParameter("to")

不过,请确保在代码中的某处包含发件人的电子邮件,以便您可以在必要时与他们联系。

还有一些其他的考虑:

  • 请阅读有关从 JSP 页面中分离实际 Java 代码的信息,您可以在 google 上找到足够的链接。
  • 您正在导入类,但不再使用导入。
  • 代码中有无用%><%的,我什至不知道它们在哪里。
于 2013-07-06T18:36:49.327 回答