发送邮件到 Gmail 帐户
我正在从我的 Java 应用程序向 Gmail 帐户发送邮件。我使用过 Java Mail API,它工作得很好。但是,是否可以在不使用java中的邮件API的情况下发送电子邮件呢?
我的意思是仅使用套接字:
public class Main {
public static void main(String[] args) throws Exception {
String host = "smtp.gmail.com";
int port = 465;
String from = "[email protected]";
String toAddr = "[email protected]";
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;
}
}
}
但它不起作用,它不发送邮件。谁能告诉我可能是什么问题?
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 = "[email protected]";
String toAddr = "[email protected]";
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个很好的例子:
-> http://www.java2s.com/Code/Java/Network-Protocol /SendingMailUsingSockets.htm
Here is a good example:
-> http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm
如果您有动态 IP,您可能无法向 Googlemail 发送邮件。
If you have a dynamic IP you are probably not able to send messages to Googlemail.
GMail 不允许非安全邮件传输。您需要在实施中建立 SSL/TLS 连接。
GMail doesn't allow non-secure mail transfer. You need to make an SSL/TLS connection in your implementation.
默认情况下,Google 仅允许加密连接。在我看来,这实际上是一件好事。
但如果您必须以纯文本形式发送邮件,则可以在帐户页面上启用它在设置下。
By default, Google only allows encrypted connections. That's actually a good thing, in my opinion.
But if you must send your mails in plaintext, you can enable it on your Account Page under settings.
为了使用安全连接,请使用 SSLSocket 而不是 Socket,如下所示:
In order to use a secure connection, use SSLSocket instead of Socket, like this: