发送邮件到Gmail帐户 [英] send mail to Gmail account
本文介绍了发送邮件到Gmail帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从我的Java应用程序向Gmail帐户发送邮件。我曾使用Java邮件API,它工作正常。但是,是否可以在不使用Java中的邮件API的情况下发送电子邮件?
我的意思是使用套接字:
public class Main {
public static void main(String [] args)throws Exception {
String host =smtp.gmail.com;
int port = 465;
String =sh2rpzain@gmail.com;
String toAddr =sharpzian@gmail.com;
Socket servSocket = new Socket(host,port);
DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
DataInputStream is = new DataInputStream(servSocket.getInputStream());
if(servSocket!= null&& os!= null&& is!= null){
os.writeBytes(HELO\r\\\
) ;
os.writeBytes(MAIL From:+ from +\r\\\
);
os.writeBytes(RCPT To:+ toAddr +\r\\\
);
os.writeBytes(DATA \r\\\
);
os.writeBytes(X-Mailer:Java \r\\\
);
os.writeBytes(DATE:+ DateFormat.getDateInstance(DateFormat.FULL,
Locale.US).format(new Date())+\r\\\
);
os.writeBytes(From:+ from +\r\\\
);
os.writeBytes(To:+ toAddr +\r\\\
);
}
os.writeBytes(Subject:\r\\\
);
os.writeBytes(body \r\\\
);
os.writeBytes(\r\\\
.\r\\\
);
os.writeBytes(QUIT \r\\\
);
字符串响应; ((responseline = is.readUTF())!= null){
if(responseline.indexOf(Ok)!= -1)
break;
}
}
}
但它不起作用,它不发送邮件。谁能告诉我可能是什么问题?
b $ b public class SMTPDemo {
public static void main(String args [])throws IOException,
UnknownHostException {
String msgFile =file.txt;
String =java2s@java2s.com;
String to =yourEmail@yourServer.com;
String mailHost =yourHost;
SMTP邮件=新SMTP(mailHost);
if(mail!= null){
if(mail.send(new FileReader(msgFile),from,to)){
System.out.println(Mail sent。) ;
} else {
System.out.println(连接到SMTP服务器失败!);
}
}
System.out.println(完成。);
}
静态类SMTP {
private final static int SMTP_PORT = 25;
InetAddress mailHost;
InetAddress localhost;
BufferedReader in;
PrintWriter out;
public SMTP(String host)throws UnknownHostException {
mailHost = InetAddress.getByName(host);
localhost = InetAddress.getLocalHost();
System.out.println(mailhost =+ mailHost);
System.out.println(localhost =+ localhost);
System.out.println(SMTP构造函数done \\\
);
$ b $ public boolean send(FileReader msgFileReader,String from,String to)
抛出IOException {
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader味精;
msg =新的BufferedReader(msgFileReader);
smtpPipe =新的套接字(mailHost,SMTP_PORT);
if(smtpPipe == null){
return false;
}
inn = smtpPipe.getInputStream();
outt = smtpPipe.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt),true);
if(inn == null || outt == null){
System.out.println(无法打开流到套接字。);
返回false;
}
String initialID = in.readLine();
System.out.println(initialID);
System.out.println(HELO+ localhost.getHostName());
out.println(HELO+ localhost.getHostName());
String welcome = in.readLine();
System.out.println(欢迎);
System.out.println(MAIL From:<+ from +>);
out.println(MAIL From:<+ from +>);
字符串senderOK = in.readLine();
System.out.println(senderOK);
System.out.println(RCPT TO:<+ to +>);
out.println(RCPT TO:<+ to +>);
字符串recipientOK = in.readLine();
System.out.println(recipientOK);
System.out.println(DATA);
out.println(DATA);
字符串行; ((line = msg.readLine())!= null){
out.println(line);
while
}
System.out.println(。);
out.println(。);
String acceptedOK = in.readLine();
System.out.println(acceptedOK);
System.out.println(QUIT);
out.println(QUIT);
返回true;
- > http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm
I am sending mail from my Java app to Gmail Account. I had used the Java Mail API and it worked fine. But is it possible to send an e-mail without using the mail API in java?
I mean just by using sockets:
public class Main {
public static void main(String[] args) throws Exception {
String host = "smtp.gmail.com";
int port = 465;
String from = "sh2rpzain@gmail.com";
String toAddr = "sharpzian@gmail.com";
Socket servSocket = new Socket(host, port);
DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
DataInputStream is = new DataInputStream(servSocket.getInputStream());
if (servSocket != null && os != null && is != null) {
os.writeBytes("HELO\r\n");
os.writeBytes("MAIL From:" + from + " \r\n");
os.writeBytes("RCPT To:" + toAddr + "\r\n");
os.writeBytes("DATA\r\n");
os.writeBytes("X-Mailer: Java\r\n");
os.writeBytes("DATE: " + DateFormat.getDateInstance(DateFormat.FULL,
Locale.US).format(new Date()) + "\r\n");
os.writeBytes("From:" + from + "\r\n");
os.writeBytes("To:" + toAddr + "\r\n");
}
os.writeBytes("Subject:\r\n");
os.writeBytes("body\r\n");
os.writeBytes("\r\n.\r\n");
os.writeBytes("QUIT\r\n");
String responseline;
while ((responseline = is.readUTF()) != null) {
if (responseline.indexOf("Ok") != -1)
break;
}
}
}
But it is not working, it doesn't send out the mail. Can anyone tell me what could be the problem?
解决方案
Here is a good example:
public class SMTPDemo {
public static void main(String args[]) throws IOException,
UnknownHostException {
String msgFile = "file.txt";
String from = "java2s@java2s.com";
String to = "yourEmail@yourServer.com";
String mailHost = "yourHost";
SMTP mail = new SMTP(mailHost);
if (mail != null) {
if (mail.send(new FileReader(msgFile), from, to)) {
System.out.println("Mail sent.");
} else {
System.out.println("Connect to SMTP server failed!");
}
}
System.out.println("Done.");
}
static class SMTP {
private final static int SMTP_PORT = 25;
InetAddress mailHost;
InetAddress localhost;
BufferedReader in;
PrintWriter out;
public SMTP(String host) throws UnknownHostException {
mailHost = InetAddress.getByName(host);
localhost = InetAddress.getLocalHost();
System.out.println("mailhost = " + mailHost);
System.out.println("localhost= " + localhost);
System.out.println("SMTP constructor done\n");
}
public boolean send(FileReader msgFileReader, String from, String to)
throws IOException {
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader msg;
msg = new BufferedReader(msgFileReader);
smtpPipe = new Socket(mailHost, SMTP_PORT);
if (smtpPipe == null) {
return false;
}
inn = smtpPipe.getInputStream();
outt = smtpPipe.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);
if (inn == null || outt == null) {
System.out.println("Failed to open streams to socket.");
return false;
}
String initialID = in.readLine();
System.out.println(initialID);
System.out.println("HELO " + localhost.getHostName());
out.println("HELO " + localhost.getHostName());
String welcome = in.readLine();
System.out.println(welcome);
System.out.println("MAIL From:<" + from + ">");
out.println("MAIL From:<" + from + ">");
String senderOK = in.readLine();
System.out.println(senderOK);
System.out.println("RCPT TO:<" + to + ">");
out.println("RCPT TO:<" + to + ">");
String recipientOK = in.readLine();
System.out.println(recipientOK);
System.out.println("DATA");
out.println("DATA");
String line;
while ((line = msg.readLine()) != null) {
out.println(line);
}
System.out.println(".");
out.println(".");
String acceptedOK = in.readLine();
System.out.println(acceptedOK);
System.out.println("QUIT");
out.println("QUIT");
return true;
}
}
}
-> http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm
这篇关于发送邮件到Gmail帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文