⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendattachment.java

📁 Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用安全
💻 JAVA
字号:
import java.util.Properties;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;

// Chapter 13, Listing 5
public class SendAttachment 
{
	public static void main(String args[])
	{
		int argc = args.length;

		// Check for valid number of parameters
		if (argc != 3)
		{
			System.out.println ("Syntax :");
			System.out.println ("java SendAttachment smtphost to_address filepath");
			return;
		}

		String host = args[0];
		String to   = args[1];
		String file = args[2];

		// Create a properties file, specifying mail settings
		Properties prop = new Properties();
		prop.put ("mail.transport.default", "smtp");
		prop.put ("mail.smtp.host", host);		


		try
		{
			// Get a session, with the specified properties
			Session mySession = Session.getInstance (prop, null);

			System.out.println ("Composing message");

			// Create a message to send, specifying our session
			Message message = new MimeMessage (mySession);
			message.setSubject ("File attachment file");			

			// Create an InternetAddress, for specifying recipient
			InternetAddress toAddr  = new InternetAddress ( to );
			message.setRecipient (Message.RecipientType.TO, toAddr);

			// Use the same sender address as recipient			
			message.setFrom (toAddr);

			// Construct a multipart message
			Multipart part = new MimeMultipart();

			// Create the message body
			BodyPart  msgBody = new MimeBodyPart ();
			msgBody.setText ("There is a file attached to this message....");

			// Create the message attachment
			BodyPart  attachment = new MimeBodyPart();
			
			// Use a file data source for reading the file
			FileDataSource fileDataSource = new FileDataSource(file);

			// Set the appropriate data handler
			attachment.setDataHandler ( new DataHandler ( fileDataSource ) );

			// Set the filename of the file (don't include path info)
			if (file.indexOf( File.separator ) != -1)
			{
				String fileName = file.substring 
					(file.lastIndexOf( File.separator)+1, file.length());
				attachment.setFileName(fileName);
			}
			else
			{
				attachment.setFileName(file);
			}

			System.out.println ("Adding attachments");

			// Add msg body and attachment to multipart message
			part.addBodyPart(msgBody);
			part.addBodyPart(attachment);

			// Set our multipart message as the msg contents
			message.setContent (part);

			System.out.println ("Sending message");

			// Send the message
			Transport.send(message);		

			System.out.println ("Message sent");
		}
		catch (AddressException   ae)
		{
			System.err.println ("Invalid address " + ae);
		}
		catch (MessagingException me)
		{
			System.err.println ("Messaging failure : " + me);
		}
		catch (Exception ex)
		{
			System.err.println ("Failure : " + ex);
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -