Auto backup files to an IMAP mail server
sending too many mails maybe baned by your mail server,
so we can just save as drafts to bypass it.
#
whole script:
vim /etc/cron.daily/diabsk.sh
#
#!/bin/bash
# SETTING
TOEMAIL="bak@diavps.com";
PASSWORD="xxx"
COMMENT='blog database backup'
DIR='wordpress'
YYYYMMDD=`date +%Y%m%d`
SUBJECT='DiaBak_of_'${DIR}'_'${YYYYMMDD};
# END SETTING
TMP='/tmp/diabak/'${DIR}
ATTTMP='/tmp/diabakatt/'${DIR}
rm -rf $TMP
mkdir -p $TMP
cd $TMP
# Put files what you want to backup to $TMP
# Don't change anything below
rm -rf $ATTTMP
mkdir -p $ATTTMP
cd $ATTTMP
tar zcPf backup.tar.gz $TMP
rm -rf $TMP
split -b 10m -a 3 -d backup.tar.gz ${SUBJECT}.part
rm -f backup.tar.gz
for file in *
do
#echo $COMMENT | mutt -a $file -s $SUBJECT $TOEMAIL
java -jar SaveEmailtoDraft.jar -to xxx@qq.com -subject "$SUBJECT" -text xxx@vip.qq.com -smtphost smtp.qq.com -imaphost imap.qq.com -user "$TOEMAIL" -password "$PASSWORD" -filename "$file"
sleep 30s
done
rm -rf $ATTTMP
how to use save file to draft only:
java -jar SaveEmailtoDraft.jar -to xxx@qq.com -subject 'new subject test' -text xxx@vip.qq.com -smtphost smtp.qq.com -imaphost imap.qq.com -user xxx@vip.qq.com -password xxx -filename /Users/galenzhao/Desktop/EmailDraft/lib/javax.mail.jar
prebuild release: jar file
and this is the java code:
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
public class SaveEmailtoDraft {
public static void main(String[] args) {
String to = "ToEmail@XXX.com";
//String from = "FromEmail@XXXX.com";
String smtphost = "XXXX.XXXX.com";
String imapshost = null;
String imapsport = null;
String user = null;
String password = null;
String filename = "C:/temp/bb.log";
String subject = null;
String text = null;
// create the parser
CommandLineParser parser = new DefaultParser();
try {
// create Options object
Options options = new Options();
options.addOption("smtphost", true, "");
options.addOption("imaphost", true, "");
options.addOption("imapport", "");
options.addOption("user", true, "");
options.addOption("password", true, "");
options.addOption("to", true, "");
options.addOption("from", "");
options.addOption("filename", true, "");
options.addOption("subject", true, "");
options.addOption("text", true, "");
// parse the command line arguments
CommandLine line = parser.parse(options, args);
smtphost = line.getOptionValue("smtphost");
imapshost = line.getOptionValue("imaphost");
imapsport = line.getOptionValue("imapport", "993");
user = line.getOptionValue("user");
password = line.getOptionValue("password");
to = line.getOptionValue("to");
//from = line.getOptionValue("from", user);
filename = line.getOptionValue("filename");
subject = line.getOptionValue("subject");
text = line.getOptionValue("text");
} catch (org.apache.commons.cli.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", smtphost);
Session session = Session.getDefaultInstance(properties);
try {
Message message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(text);
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
if (filename != null) {
// Part two is attachment
messageBodyPart = new MimeBodyPart();
// Add the attachment file path
FileDataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
String[] path = filename.split("/");
messageBodyPart.setFileName(path[path.length - 1]);
multipart.addBodyPart(messageBodyPart);
}
// Send the complete message parts
message.setContent(multipart);
properties.setProperty("mail.store.protocol", "imaps");
properties.setProperty("mail.imaps.port", imapsport);
session = Session.getDefaultInstance(properties, null);
javax.mail.Store store = session.getStore("imaps");
store.connect(imapshost, user, password);
Folder draft = store.getFolder("drafts");
draft.open(Folder.READ_WRITE);
draft.appendMessages(new Message[] { message });
System.out.println(draft.getFullName() + " has : " + draft.getMessageCount() + " Message(s)");
} catch (MessagingException mex) {
mex.printStackTrace();
}
//return 0;
}
}
#
https://www.vpser.net/security/vps-auto-bakup-send-by-gmail.html https://www.codeproject.com/Tips/1084798/Save-Email-With-Attachment-in-Outlooks-Draft-Folde