我在此使用注册页面,我需要发送到邮件的验证链接,同时执行错误并说java.net.ConnectException:连接被拒绝:connect
所以请你帮我
register.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>REGISTRATION</title>
</head>
<body>
<%!
DBCreation creation;
Connection connection;
%>
<%
String uid=request.getParameter("userid");
String pwd=request.getParameter("pwd");
String fName=request.getParameter("fname");
String lName=request.getParameter("lname");
String email=request.getParameter("email");
String location=request.getParameter("location");
String encpwd=encryptPwd(pwd);
System.out.println(encpwd);
connection=DBCreation.getConnection();
String result;
// Recipient's email ID needs to be mentioned.
String toEmail = email;
// Sender's email ID needs to be mentioned
String fromEmail = "nbarath2008@gmail.com";
// Assuming you are sending email from localhost
//String host = "localhost";
// Get system properties object
Properties properties = System.getProperties();
// Setup mail server
// properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session mailSession = Session.getDefaultInstance(properties);
Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));
transport.connect("localhost",25,null,null);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(mailSession);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,""+email);
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>This is actual message</h1>",
"text/html" );
// Send message
Transport.send(message);
result = "Sent message successfully....";
PreparedStatement statement=connection.prepareStatement("insert into userregistration values(?,?,?,?,?,?,?)");
statement.setString(1,uid);
statement.setString(2,encpwd);
statement.setString(3,fName);
statement.setString(4, lName);
statement.setString(5, location);
statement.setString(6, fromEmail);
statement.setString(7, toEmail);
int i=statement.executeUpdate();
if(i>0)
{
// Send message
Transport.send(message);
result = "Sent message successfully....";
RequestDispatcher rd=request.getRequestDispatcher("regsuccess.jsp");
rd.forward(request, response);
}
}catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
最佳答案
这是工作代码。请问您有任何疑问!
导入java.util .;
导入javax.mail。
导入javax.mail.internet。*;
public class Main {
private static String USER_NAME = "gmail-user-name"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "******"; // Enter your GMail password
private static String RECIPIENT = "RECIPIENT EMAIL address";
public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
sendFromGMail(from, pass, to, subject, body);
}
private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
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");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
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++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}