How does this e-mail program work?
I read that email program contact's the author's ISP email server to pass it the message.
In the following program i make a successfull connection with SMTP
server of gmail
.I wanted to send the email from this program ( from gmail account) to rediff account.No where in this program i open the connection to rediff server . But i am successfull in sending the emails. How is that ? I just tested by changing the to address from gmail
to rediff and was successful. Here is the program :
// cross mail
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
class crossmail {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.stmp.user", "from");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new Athenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "from";
String password = "paassword";
return new PasswordAuthentication("from", "paassword");
}
});
String to = "me@rediff";
String from = "from@gmail.com";
String subject = "Testing...";
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(t开发者_JAVA技巧o));
msg.setSubject(subject);
msg.setText("Test Successfull...!");
Transport transport = session.getTransport("smtp");
transport.send(msg);
System.out.println("fine!!");
} catch(Exception exc) {
System.out.println(exc);
}
}
}
How does this email program work ? If i am wrong in my understanding please correct that.
Email apps RARELY directly contact the recipient's email server. That's not what they're for. Instead, they'll contact your local SMTP server (running locally on the same server, or your ISP's) and hand off the mail to that server. That intermediate server then does the hard work of looking up who handles the recipient's email and contacts that server.
Part of that process is to look at the recipient's email address, say fred@example.com, and do a DNS lookup on example.com
to get that domain's MX records. Those (M)ail e(X)changer records specify the "public" mail servers for the domain, where incoming mail should be sent.
This frees you and your app from ever having to know HOW or WHERE to deliver every piece of mail... all you have to do is know where your local mail server is. Much like you don't need to know HOW a snailmail letter goes from you to your penpal - all you is write an address on the envelope and drop it into a mailbox. The postal service takes care of all the details in getting the letter from point A to point B.
The program logs into the gmail SMTP server and passes it the message. The gmail server then forwards it onto the recipients email server.
SMTP - Simple Mail Transfer Protocol
Email servers are basically relay stations between users
I send to my email server, it then looks up your email server and sends the message there, you get the message from your email server.
精彩评论