<%@ page language="java" import="java.util.Properties,javax.mail.*,javax.mail.internet.InternetAddress,javax.mail.internet.MimeMessage" %>
public class SendAttachment {

    public static void main(String args[]) throws Exception {
        String host = "smtp.gmail.com";
        String from = "123.digit@gmail.com";
        String to = "ae@gmail.com";

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.setProperty("mail.smtp.host", host);

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);

        // Define message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
                new InternetAddress(to));
        message.setSubject("JavaMail Attachment");

        // Create the message part 
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message

        Multipart multipart = new MimeMultipart();

        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        String filename = "1.csv";
        DataSource source = new FileDataSource("d:\\csv\\1.csv");
        messageBodyPart.setDataHandler(new DataHandler(source));

        // Put parts in message

        // Send the message
     System.out.println("Msg Send ....");

i m not able to send mail with this code anyone can make it work

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

7 Years
Discussion Span
Last Post by jwenting

he's using Java code in JSP. That's the first error.
Use a servlet insted for sending that email.

Then he's stupidly trying to create a class inside a JSP. A class that has a main method which he no doubt expects to be called and execute that main method when he calls the JSP from his browser.

And then he seems to assume that Google runs gmail as an open mail relay he can just use for his private email traffic, something I highly doubt is the case (Google makes money selling your email address to spammers maybe, but they don't want to accommodate spammers by opening their servers to them).

That's enough basic flaws even looking at the actual code to send that email isn't necessary.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.