配置jboss以发送电子邮件
我想从jboss中的servlet发送一封电子邮件。我对jboss mail-service.xml文件进行了更改。这是我的servlet代码配置jboss以发送电子邮件,jboss,smtp,jakarta-mail,Jboss,Smtp,Jakarta Mail,我想从jboss中的servlet发送一封电子邮件。我对jboss mail-service.xml文件进行了更改。这是我的servlet代码 import java.io.*; import java.net.*; import java.util.Properties; import javax.mail.AuthenticationFailedException; import javax.mail.Authenticator; import javax.mail.PasswordAut
import java.io.*;
import java.net.*;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;
import javax.servlet.http.*;
public class Email extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
final String err = "/error.jsp";
final String succ = "/success.jsp";
String from = "*******@gmail.com";
String to = "*******@yahoo.in";
String subject = "from servlet";
String message = "HI";
String login = "***@gmail.com";
String password = "*******";
try {
Properties props = new Properties();
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
Transport.send(msg);
} catch (AuthenticationFailedException ex) {
/*request.setAttribute("ErrorMessage", "Authentication failed");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);*/
System.out.println("Authentication failed");
} catch (AddressException ex) {
/*request.setAttribute("ErrorMessage", "Wrong email address");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);*/
System.out.println("Wrong Email Address");
} catch (MessagingException ex) {
/*request.setAttribute("ErrorMessage", ex.getMessage());
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);*/
System.out.println("asdsad"+ex.getMessage());
}
/*RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
dispatcher.forward(request, response);*/
System.out.println("Success");
}
private class SMTPAuthenticator extends Authenticator {
private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password) {
authentication = new PasswordAuthentication(login, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
这是我的mail-service.xml文件
mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
<attribute name="JNDIName">java:/Mail</attribute>
<attribute name="User">*****@gmail.com</attribute>
<attribute name="Password">******</attribute>
<attribute name="Configuration">
<!-- A test configuration -->
<configuration>
<!-- Change to your mail server prototocol -->
<property name="mail.store.protocol" value="pop3" />
<property name="mail.transport.protocol" value="smtp" />
<!-- Change to the user who will receive mail -->
<property name="mail.user" value="nobody" />
<!-- Change to the mail server -->
<property name="mail.pop3.host" value="pop.gmail.com" />
<!-- Change to the SMTP gateway server -->
<property name="mail.smtp.host" value="smtp.gmail.com" />
<property name="mail.smtp.auth" value="true" />
<property name="mail.smtp.user" value="******@gmail.com" />
<property name="mail.smtp.password" value="******" />
<property name="mail.smtp.ssl.enable" value="true" />
<property name="mail.smtp.starttls.enable" value="true" />
<property name="mail.smtp.socketFactory.class"
value="javax.net.ssl.SSLSocketFactory" />
<!-- The mail server port -->
<property name="mail.smtp.port" value="465" />
<!-- Change the default address mail will be from -->
<property name="mail.from" value="ashwinbhskr@gmail.com" />
<!-- Enable debugging output from the javamail classes -->
<property name="mail.debug" value="false" />
</configuration>
</attribute>
<depends>jboss:service=Naming</depends>
我哪里出错了?mailservice.xml中没有任何更改。在servlet的doPost方法中,删除所有其他内容并粘贴此内容。我希望它对你有用
String host = "smtp.gmail.com";
String from = "user@gmail.com";
String pass = "*******";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"usr@yahoo.in"}; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
String host=“smtp.gmail.com”;
字符串from=”user@gmail.com";
字符串传递=“*******”;
Properties props=System.getProperties();
props.put(“mail.smtp.starttls.enable”,“true”);//增加了这一行
props.put(“mail.smtp.host”,host);
props.put(“mail.smtp.user”,from);
props.put(“mail.smtp.password”,pass);
props.put(“mail.smtp.port”,“587”);
props.put(“mail.smtp.auth”,“true”);
字符串[]到={”usr@yahoo.in"}; // 增加了这一行
Session Session=Session.getDefaultInstance(props,null);
MimeMessage message=新MimeMessage(会话);
message.setFrom(新的InternetAddress(from));
InternetAddress[]toAddress=新的InternetAddress[to.length];
//获取地址数组的步骤
对于(int i=0;i
嘿,我不知道你们两个想在这里做什么,但我们已经注意到了。所以,请把它删掉。如何在mail-service.xml中输入和读取收件人地址?
String host = "smtp.gmail.com";
String from = "user@gmail.com";
String pass = "*******";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"usr@yahoo.in"}; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();