English 中文(简体)
通过java邮件寄送垃圾邮件
原标题:problem through sending mesaage by jsp by java mail
  • 时间:2011-03-17 08:10:27
  •  标签:
  • html
  • jsp
enter code here

It s MailForm.html

<body>
  <form action="sendMail.jsp" method="post">
    <table cellspacing="2" cellpadding="2" border="1">
      <tr>
        <td>To:</td>
        <td>
          <input type="text" name="to" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>From:</td>
        <td>
          <input type="text" name="from" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>Subject</td>
        <td>
          <input type="text" name="subject" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <textarea cols="40" rows="10" name="body"></textarea>
        </td>
      </tr>
      <tr>
        <td>
          <input type="submit" name="submit" value="Submit">
          <input type="Reset">
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


----------------------------------

    enter code here

It s SendMail.jsp JSP JavaMail Example

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%
    String host = "smtp.gmail.com";
    String to = request.getParameter("to");
    String from = request.getParameter("from");
    String subject = request.getParameter("subject");
    String messageText = request.getParameter("body");
    boolean sessionDebug = false;

    Properties props = System.getProperties();
    props.put("mail.host", host);
    props.put("mail.transport.protocol", "smtp");

    Session mailSession = Session.getDefaultInstance(props, null);

    mailSession.setDebug(sessionDebug);

    Message msg = new MimeMessage(mailSession);

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

    out.println("Mail was sent to " + to);
    out.println(" from " + from);
    out.println(" using host " + host + ".");

%>
    </table>
  </body>
</html>
最佳回答

该法典的工作

String host = "smtp.gmail.com";
user="youEmail@gmail.com";
pass="yourPassword";

 String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = email_to_address; // out going email id
String from = email_from_address; //Email id of the recipient
String subject = email_subject;
String messageText = email_body;
boolean sessionDebug = true;


Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);


 Session mailSession = Session.getDefaultInstance(props, null);
 mailSession.setDebug(sessionDebug);


 Message msg = new MimeMessage(mailSession);
 msg.setFrom(new InternetAddress(from));
 InternetAddress[] address = {new InternetAddress(to)};
 msg.setRecipients(Message.RecipientType.TO, address);
 msg.setSubject(subject);
 msg.setContent(messageText, "text/html"); // use setText if you want to send text


Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);


try {
transport.sendMessage(msg, msg.getAllRecipients());
out.println("Send Success");
WasEmailSent = true; // assume it was sent
}

catch (Exception err) {
WasEmailSent = false; // assume it s a fail
}
transport.close();
问题回答

尝试你的法典中的词汇

Properties = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

together with these you also need to pass the authenticator to the Session.getDefaultInstance

喜欢

Session mailSession = Session.getDefaultInstance(props, {pass authenticator here});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签