2016-03-10 3 views
0

Я пытаюсь отправить электронное письмо с одной учетной записи на другую с помощью javamail. Но код не может выполнить Transport.send (msg); линия. Что может быть возможной причиной? Ниже приведен код jsp.Transport.send (msg) не работает

<% 

     String host = "localhost"; 
     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); 

     try { 
     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 + "."); 
     } catch (MessagingException mex) {mex.printStackTrace();} 
    %> 
+2

StackTrace please –

+2

Не писать код java на странице jsp – soorapadman

+0

Где находится ваш порт smtp? в любом случае положил ваш StackTrace здесь. –

ответ

2

Эти свойства, которые я обычно использую

properties.put("mail.transport.protocol", "smtp"); 
properties.put("mail.smtp.host", host); 
properties.put("mail.user", from); 
properties.put("mail.smtp.port", smtpPort); 
properties.put("mail.smtp.localhost", "myHost"); 

Session session = Session.getInstance(properties, null); 

Вы заметите, что я поставил mail.smtp.localhost, который должен быть установлен, если имя хоста машины не настроен должным образом, например, для виртуальной машины и т. д.

Смежные вопросы