红帽云邮 Unimarketing 邮件营销 EDM

API开发指南( Java 客户端DEMO

第一步: 注册为红帽云邮用户

  • 先注册会员账号,已注册用户不需要注册。


第二步:登录本系统,取得API Key

  • 使用用户名与密码登录本系统,进入【系统设置】➞ 找到【API Key管理】。
  • 找到API Key的Consumer Key和Consumer Secret后,就可以调用SMTP和RESTful接口了。

第三步: 使用API Key调用SMTP接口

  • 1. 使用邮件客户端调用SMTP接口,直接进行邮件发送调用,下面用户名密码就是API Key的Consumer Key和Consumer Secret:


  • 或者:

    import java.util.Properties;
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.internet.MimeUtility;
    import org.apache.commons.lang3.StringUtils;
    
    public class MySMTP {
    
    	private static final String SMTP_SERVER = "smtp.unimarketing.org";
    	private static final String APIKey = "您的API Key";
    	private static final String APISecret = "您的API Secret";
    
    	public static void main(String[] args) throws Exception {
    
    		Properties properties = new Properties();
    		properties.put("mail.smtp.host", SMTP_SERVER);
    		properties.put("mail.smtp.auth", "true");
    		properties.put("mail.smtp.port", "25");
    		properties.put("mail.transport.protocol", "smtp");
    
    		String fromAddress    = "no-reply@example.com";
    		String recipient      = "someone@customer.com";
    		String subject        = "邮件主题";
    		String fileAttachment = "附件路径";
    		String content        = "邮件正文内容";
    
    		InternetAddress[] receiveAddresses = new InternetAddress[] { new InternetAddress(recipient) };
    
    		Session session = Session.getInstance(properties, new SmtpAuth());
    		session.setDebug(true);
            	session.setDebugOut(new PrintStream("smtp.log"));
    
    		MimeMessage message = new MimeMessage(session);
    		MimeMultipart multipart = new MimeMultipart();
    
    		multipart.setSubType(ContentTypeEnum.ALTERNATIVE.getLabel());
    		MimeBodyPart bodyPart = new MimeBodyPart();
    		bodyPart.setContent(content, "text/html; charset=utf-8");
    		multipart.addBodyPart(bodyPart);
    
    		if (StringUtils.isNotBlank(fileAttachment)) {
    			DataSource dataSource = new FileDataSource(fileAttachment);
    			String name = dataSource.getName();
    			bodyPart = new MimeBodyPart();
    			bodyPart.setDataHandler(new DataHandler(dataSource));
    			bodyPart.setFileName(MimeUtility.encodeText(name));
    			multipart.addBodyPart(bodyPart);
    		}
    
    		message.setSubject(subject);
    		message.setContent(multipart);
    
    		message.setFrom(new InternetAddress(fromAddress));
    		message.setRecipients(Message.RecipientType.TO, receiveAddresses);
    		InternetAddress[] addresses = new InternetAddress[1];
    		addresses[0] = new InternetAddress("someone@somewhere.com");
    		message.setReplyTo(addresses);
    
    		message.addHeader("X-List", MimeUtility.encodeText("联系人列表名称"));
    		message.addHeader("X-Campaign", MimeUtility.encodeText("邮件名称"));
    		Transport.send(message);
    
     /**
       *  获取返回信息
       */
            File file = new File("smtp.log");
            BufferedReader reader = null;
    		try {
    			reader = new BufferedReader(new FileReader(file));
    			String tempString = null;
    			while ((tempString = reader.readLine()) != null) {
    				if (tempString.lastIndexOf("250 ok:") != -1) {
    					System.out.println("返回数据为:" + tempString);
    				}
    			}
    			reader.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (reader != null) {
    				reader.close();
    			}
    		}
    
    		System.out.println("发送成功");
    	}
    	
    	static class SmtpAuth extends Authenticator {
    		
    		public PasswordAuthentication getPasswordAuthentication() {
    			return new PasswordAuthentication(USER_NAME, PASSWORD);
    		}
    	}
    }
    
  • 2. 使用RESTfull接口进行邮件发送调用:

    	UnimailClient unimailClient = UnimailClient.getClient(false, UnimailClient.AuthMode.APIKEY, new String[] { "您的API Key", "您的API Secret" });
    	MessageSendReq msr = new MessageSendReq();
    	msr.setSubject("注册激活通知");		
    	msr.setFrom("no-reply@customer.com");	//【选填】From 地址
    	msr.setReply("services@example.com");	//【选填】回复地址
    	msr.setListName("注册激活列表");		//【必填】列表名称
    	msr.setContent("邮件正文内容");		// 邮件内容
    	msr.setContentType("html");        	// 邮件类型
    	msr.setTo("someone@exanple.com");    	// 收件人
    	msr.setQos(MessageSendReq.QOS_BULK); 	// 非触发交易邮件
    	msr.setMessageName("注册激活通知");   	// 邮件名称
    
    	//发送邮件
    	List trsLists = unimailClient.messageSend(msr);
    	if (trsLists != null) {
    		//获取Message-Id,做后续处理等
    		for (TransactionMailRes transactionMailRes : trsLists) {
    			log.debug(ToStringBuilder.reflectionToString(transactionMailRes,ToStringStyle.MULTI_LINE_STYLE));	
    		}
    	}
    

版权所有 Copyright 2010-2021  厦门硕顿信息技术有限公司技有限公司 - 闽ICP备1608763号