从不在jsp中工作的地址发送邮件表单反馈表

时间:2015-08-01 11:06:53

标签: java jsp email

我正在尝试反馈表单,以便用户在邮件中发送反馈信息。在jsp中的代码发送邮件给收件人,现在我正在尝试使用静态地址。但我想要用户地址(来自地址)。

       <%@ page language="java" 
        contentType="text/html; 
        charset=windows-1256"
         pageEncoding="windows-1256"         
         import="java.net.UnknownHostException"         
    %> 
    <%@ page import="java.io.*"%>
    <%@ page import="java.util.Date"%>
    <%@ page import="java.util.Random"%>
    <%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>

      <%           

        try{
        String report = request.getParameter("report"); 
        String qus = request.getParameter("qus"); 
        String category = request.getParameter("category"); 
        String email="something@gmail.com";
        String messageText = "feedback message sample";

     String host = "smtp.gmail.com";
     String user = "abc@gmail.com"; //Enter some valid gmail Id for verification
     String pass = "*******";                  // Enter password here
     String from = "fromaddress@gmail.com"; 
     String subject = "Report of Error"; 

     boolean sessionDebug = false; 

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

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

    Message msg = new MimeMessage(mailSession);
    InternetAddress fromaddress = new InternetAddress(from); 
    msg.setFrom(new InternetAddress(from));


     InternetAddress[] address = {new InternetAddress(email)}; 
     msg.setRecipients(Message.RecipientType.TO, address); 
     msg.setSubject(subject);
     msg.setText(messageText);
     msg.saveChanges();
    Transport transport = mailSession.getTransport("smtps");
    transport.connect(host, user, pass); 
    transport.sendMessage(msg, msg.getAllRecipients());

        }catch(Exception e)
        {
           out.println(e);
        } 
    %>

邮件已发送但是它来自abc@gmail.com而不是来自“fromaddress@gmail.com”我尝试了msg.setFrom()但是这不起作用。我希望邮件从“fromaddress @”发送gmail.com“不是来自经过身份验证的身份证明。请告诉我如何解决这个问题。

0 个答案:

没有答案