Question : How to send "tiny" email from command line (via Gmail)?

Hi all,

I'd like to use bmail http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm or similar to send a very short, simple plain text mail (admin notification) to another email address.

From what I read on Google, one has to use the following parms:

smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587

...and account name / password as well.

...OK....

But how to configure this with a simple command line tool such as bmail?
Any other tiny!!! solution???

Thanks.

Answer : How to send "tiny" email from command line (via Gmail)?

Ok I ran into some Java code this morning that I turned into a simple class to send mail through Gmail. I tested it and it seems work fine with my gmail account. It requires the javamail library which you can download from here:

http://java.sun.com/products/javamail/downloads/index.html

Just unzip the mail.jar file and include that in your classpath when you compile and run it. To compile put the GmailSender.java and mail.jar files in the same directory and type this::

# javac -cp mail.jar;. GmailSender.java

Then to run it, do this:

# java -cp mail.jar;. GmailSender <yourgmailaddress> <yourgmailpassword> <to-emailaddr> "your subject here" filename.txt

filename.txt is a text file that has the body of the email you want to send. Currently it's set for HTML email, so you can put html code in there if you want.

Keep in mind, I'm not a programmer, I just know enough to be dangerous, so this can probably be written much more efficiently and safely, but it does work.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
import java.util.Properties;
import javax.mail.Session;
import javax.mail.AuthenticationFailedException;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
 
public class GmailSender {
 
	private String SMTP_HOST_NAME = "smtp.gmail.com";
	private int SMTP_HOST_PORT = 465;
	private String SMTP_AUTH_USER;
	private String SMTP_AUTH_PWD;
	private String SMTP_TO;
	private String SMTP_SUB;
	private String SMTP_BODY;
 
	public static void main(String[] args) throws Exception {
		if (args.length < 5){
			System.err.println("Usage: GmailSender <gmail-user-name> <gmail-password> <to> <subject> <file-body>");
			System.exit(1);
		}
 
		try {
			new GmailSender(args);
		} catch (AuthenticationFailedException e) {
			System.err.println("Your name/password is wrong, try again. <"+e.getMessage()+">");
		} catch (Exception e) {
			System.err.println("Problems: " + e.getMessage());
		}
	}
 
	public GmailSender(String[] args) throws Exception {
		SMTP_AUTH_USER = args[0];
		SMTP_AUTH_PWD = args[1];
		SMTP_TO = args[2];
		SMTP_SUB = args[3];
		SMTP_BODY = "";
		BufferedReader in = new BufferedReader(
			new InputStreamReader(
				new DataInputStream(
					new FileInputStream(args[4])
				)
			)
		);
		String line;
		while ((line = in.readLine()) != null){ 
			SMTP_BODY += line + "\n";
		}
		in.close();
 
		Properties props = new Properties();
 
		props.put("mail.transport.protocol", "smtps");
		props.put("mail.smtps.host", SMTP_HOST_NAME);
		props.put("mail.smtps.auth", "true");
		props.put("mail.smtps.quitwait", "false");
 
		Session mailSession = Session.getDefaultInstance(props);
		//mailSession.setDebug(true);
		Transport transport = mailSession.getTransport();
 
		MimeMessage message = new MimeMessage(mailSession);
		message.setSubject(SMTP_SUB);
		message.setContent(SMTP_BODY, "text/html");
 
		message.addRecipient(Message.RecipientType.TO,
			 new InternetAddress(SMTP_TO));
 
		transport.connect
		  (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
 
		transport.sendMessage(message,
			message.getRecipients(Message.RecipientType.TO));
		transport.close();
		System.out.println("Successfully submitted email to " + SMTP_TO);
	}
}
 
Rename the to .java
 
Random Solutions  
 
programming4us programming4us