<%@ 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));
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
        message.setSubject("JavaMail Attachment");

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

        // Fill the message
        messageBodyPart.setText("hi");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

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

        // Put parts in message
        message.setContent(multipart);

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

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

Recommended Answers

All 2 Replies

wht error you are getting........

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.