我正在使用JavaMail – GMail
通过TLS
方法从下方链接发送来自java应用程序的邮件。
当我尝试这样做时,我收到以下异常,所以有人可以告诉我,为了发送电子邮件,我需要做些什么来成功连接。
javamail-api-sending-email-via-gmail-smtp-example link
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 905, response: -1
at com.pr.SendMailTLS.main(SendMailTLS.java:48)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 905, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1215)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
at javax.mail.Service.connect(Service.java:258)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.pr.SendMailTLS.main(SendMailTLS.java:43)
答案 0 :(得分:2)
你需要记下这个:
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class QuizGrading
{
public static void main (String[]args) throws IOException
{
///key part
System.out.println(" ");
BufferedReader bf = new BufferedReader(new FileReader("key.in"));
StringTokenizer st = new StringTokenizer(bf.readLine()); **(NULL)**
int num = Integer.parseInt(st.nextToken());
int [] key = new int[num];
st=new StringTokenizer(bf.readLine());
for (int i=0; i<num; i++)
{
int an = Integer.parseInt(st.nextToken());
key[i] = an;
}
///answer part
BufferedReader br = new BufferedReader(new FileReader("tenQuizzes.in"));
StringTokenizer answers = new StringTokenizer(br.readLine());
int stu=1;
int numq = Integer.parseInt(answers.nextToken());
for (int u=0; u<numq; u++)
{
int right=0;
answers = new StringTokenizer(br.readLine());
for (int s=0; s<num; s++)
{
int ans=Integer.parseInt(answers.nextToken());
if (ans==key[s])
{
right=right+1;
}
}
double percent = right*100/num;
System.out.println("There are "+right+" questions correct.");
System.out.println("Student "+stu+" scored "+percent+"%.");
System.out.println(" ");
stu=stu+1;
}
System.out.println(" ");
}
由于您使用TLS,请使用端口 - 587。
你的java代码中的:
Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
此外,请转到此Gmail设置link.:
答案 1 :(得分:0)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendMail extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
String to=req.getParameter("to");
String subject=req.getParameter("sub");
String message=req.getParameter("msg");
Mailer.send(to,subject,message);
out.print("message has been send successfully");
out.close();
}
}
答案 2 :(得分:0)
enter code here
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mailer {
public static void send(String to,String sub,String msg)
{
String host="smtp.gmail.com";
final String user="abc@gmail.com";
final String pass="123456";
Properties props=new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
Transport.send(message);
System.out.println("done");
} catch (MessagingException e)
{
e.printStackTrace();
}
}
}